Is this a bug, or do I something Wrong with SelectionChangeCommitted?

C

Cor

Hi all,

I think I do something wrong, but I don't see what.
I have made a sample.
The sample needs only a form with a combobox and this code.
(I can tell you that it took a long time to see this, but it still can be
that I do something stupid).

When I fill a combobox (1) direct, I get after SelectionChangeCommitted the
previous selected item.
When I fill a combobox (2) using a datatable I get after
SelectionChangeCommitted the selected item (The behaviour that I suspected).

This is the text on MSDN about it,
Occurs when the selected item has changed and that change is committed.

Remarks
You can create a SelectionChangeCommitted event handler to provide special
handling for the ComboBox when the user changes the selected item in the
list.
(That did me doubt if I understood it well).

I am curious if I become crazy?
(I could find nothing about it on MSDN).

Cor

\\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 10 To 100 Step 10
Me.ComboBox1.Items.Add(i.ToString)
Next
Me.ComboBox1.SelectedIndex = 5
Dim dt As New DataTable
Dim dc As New DataColumn
Dim dr As DataRow
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "i"
dt.Columns.Add(dc)
For i = 1 To 10
dr = dt.NewRow()
dr("i") = (i * 10).ToString
dt.Rows.Add(dr)
Next i
Me.ComboBox2.DisplayMember = "i"
Me.ComboBox2.DataSource = dt
Me.ComboBox2.SelectedIndex = 5
End Sub
Private Sub ComboBox1_SelectionChangeCommitted _
(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
ComboBox1.SelectionChangeCommitted
MessageBox.Show(Me.ComboBox1.Text)
End Sub
Private Sub ComboBox2_SelectionChangeCommitted _
(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
ComboBox2.SelectionChangeCommitted
MessageBox.Show(Me.ComboBox2.Text)
End Sub
///
 
J

Jan Nielsen

Hi Cor
That is strange.
I edited your code to this
************************
Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal
e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted

MessageBox.Show("Cbo1 - Change Committed:" & Me.ComboBox1.Text)

End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

MessageBox.Show("Cbo1 - Index changed: " & Me.ComboBox1.Text)

End Sub

Private Sub ComboBox2_SelectionChangeCommitted(ByVal sender As Object, ByVal
e As System.EventArgs) Handles ComboBox2.SelectionChangeCommitted

MessageBox.Show("Cbo2 - Change Committed: " & Me.ComboBox2.Text)

End Sub

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

MessageBox.Show("Cbo2 - Index changed: " & Me.ComboBox2.Text)

End Sub

**********************************

And it seems (like you said) that the ChangeCommitted event appears before
the value actually changes when the combo is filled directly. But it works
well if you use datarows (like you said)

Then I looked in the code and the only difference I could tell was the
..Displaymember so I tried this (Just a guess)

*********************

Me.ComboBox1.DisplayMember = Me.ComboBox1.Items(0)

**********************

And then it worked fine

I am puzzled

Jan
 
C

Cor

Hi Jan,

This is a nice workaround yes.
*********************

Me.ComboBox1.DisplayMember = Me.ComboBox1.Items(0)

**********************
I did make datatables instead.

I think I bring it back again to this.

Cor
 

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