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
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