ComboBox databindng Problem

B

Bart Mermuys

Hi dbuchanan & earnest,

I have been thinking about a better aproach for the ComboBox null problem,
solutions so far always corrected the ComboBox after it went to an incorrect
state (first item selected instead of cleared text). This seems to work but
as Earnest pointed out it may cause phantom changes in a datasource bound to
SelectedValue.

I've come up with a new NComboBox posted at the bottom which can be used and
bound like any other ComboBox and it deals with the null problem at the root
causing no phantom changes. This workaround fixes both SelectedIndex and
SelectedValue by overriding OnSelectedIndexChanged where the bug is located.
It also adds the abillity for the user to press del or backspace to select
nothing.

I don't believe there is another way to do this without inheriting from a
ComboBox like NComboBox.

The problem with ComboBox and Null value is fixed in .NET2.0.

HTH,
Greetings


Imports System.Reflection

Public Class NComboBox
Inherits System.Windows.Forms.ComboBox

Shared EVENT_SELIDXCHANGED As Object

Shared Sub New()
EVENT_SELIDXCHANGED = GetType(ComboBox).GetField( _
"EVENT_SELECTEDINDEXCHANGED", _
BindingFlags.NonPublic Or BindingFlags.Static).GetValue(Nothing)
End Sub

Protected Overrides Sub OnSelectedIndexChanged(ByVal e As EventArgs)
' fire selectedvalue changed
OnSelectedValueChanged(EventArgs.Empty)

' fire selectedindex changed
Dim selEvent As EventHandler = Events(EVENT_SELIDXCHANGED)
If (Not selEvent Is Nothing) Then selEvent(Me, e)

' fix, only update currencymanager if index<>-1
If ((Not MyBase.DataManager Is Nothing) And _
(MyBase.DataManager.Position <> SelectedIndex) And _
(SelectedIndex <> -1)) Then
MyBase.DataManager.Position = SelectedIndex
End If
End Sub

Private Sub NComboBox_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles MyBase.KeyDown

If (e.KeyCode = Keys.Delete) Or (e.KeyCode = Keys.Back) Then
SelectedIndex = -1
End If
End Sub

End Class
 
B

Bart Mermuys

Hi,

Earnest said:
I thought it would be appropriate to ask about this within this discussion.
I'm new to discussion groups - so let me know if this isn't the best way
to
continue this topic.

See new subthread i started.

hth,
greetings
 
G

Guest

Hi Bart,

My Newbieness is showing...

I created a class in my project called NComboBox. I think I had to add an
Imports System.Reflection to get BindingFlags reference errors corrected.

This is basic stuff now - I bet this will work - how do you use the Modified
combobox? Can it be added somehow to the Toolbox and dropped onto a form or
can you only use it in code?

Thanks for all your help with this. It's almost there!

Best,
 
B

Bart Mermuys

Hi,
Earnest said:
Hi Bart,

My Newbieness is showing...

I created a class in my project called NComboBox. I think I had to add an
Imports System.Reflection to get BindingFlags reference errors corrected.

Yes, it was in the code i posted.
This is basic stuff now - I bet this will work - how do you use the
Modified
combobox? Can it be added somehow to the Toolbox and dropped onto a form
or
can you only use it in code?

Well, you can always use it from code and it should show up on the Toolbox
just like that, but it doesn't. The thing that seems to work for me is to
start by creating a user control:

- select all code from your NComboBox class and copy it (clipboard)
- delete your current NComboBox class
- Project -> Add User Control -> NComboBox
- Whipe out all code in the new usercontrol
- paste the NComboBox code
- compile

Now, NComboBox should be on the Toolbox under My User Controls.

hth,
greetings
 
G

Guest

Hi Bart,

Yes, er, you did include the imports... sorry about that.

Thanks very much. I've been trying to track this problem down for weeks. I'm
an MSAccess VBA programmer transitioning to VB.Net. It amazes me how much
doesn't work in .Net or requires levels of proficiency far deeper than what
was required in Access. It's like a joke from an old movie: (Nashville)

"Did you ever ask a lawyer the time of day? He told you how to build a watch
didn't he..."

So far in .Net you really need to know how to build a watch instead of
knowing how to tell time.

Enough whinning. Thanks very much for the help. I really appreciate it.

Best Wishes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top