ComboBox SelectionChangedCommitted + Setting Focus = Problem
Hi
Like the title says, the combination of the SelectionChangedCommited event, and inside the event selecting another control (focus), causes to the combo box select another item that isn't the "selected" one.
To understand the problem, i have a combo box that is databinded to a datatable, the style it's dropdownlist, and the autocomplete property it's suggest and append, when the user starts typing i can see that the correct item it's selected, then the user press the Enter key, and commit the selection, looking to the code, the selected item it's the correct one, then i do everything that i need with the item and after that (all inside the event selecction changed committed), i set the focus in another control, in this case it's a textbox. After setting the focus in the textbox in the combobox the displayed item it's not the correct/"selected" one, even if the code used the right one...
If i remove the line to set the focus in the other control, everything works has expected.
The only event that i handle of the combo box it's the SelectionChangedCommitted.
I made some tests, and when I'm "typing" in the combo, the events for index changed and value changed are all firing has expected, and only commits the selection after pressing the Enter Key, and at this point, the item selected it's the same in the 3 events. And none of the events get fired again after setting the focus in the other control...
What i'm missing here?
I can solve the problem easily using and auxiliary var that stores the index selected, and then in the Got Focus off the other control, setting the correct index...
Sample Test Code, Form with one Combo and oneTextBox.
vb.net Code:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable("Teste")
dt.Columns.Add(New DataColumn("TestColumn"))
dt.Rows.Add({"11111"})
dt.Rows.Add({"12341"})
dt.Rows.Add({"34334"})
dt.Rows.Add({"55555"})
dt.Rows.Add({"55566"})
dt.Rows.Add({"88111"})
dt.Rows.Add({"99911"})
With ComboBox1
.DropDownStyle = ComboBoxStyle.DropDownList
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.DisplayMember = "TestColumn"
.ValueMember = "TestColumn"
.DataSource = dt
End With
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
TextBox1.Text = ComboBox1.SelectedValue.ToString
'aux = ComboBox1.SelectedIndex 'WorkAround
TextBox1.Focus()
End Sub
''WorkAround
'Private aux As Integer = -1
'Private Sub TextBox1_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus
' ComboBox1.SelectedIndex = aux
'End Sub
End Class
To test, just press 5,5,5,6,ENTER. In the textbox it will have the 55566 has expected, but in the combo it will have another value... :(
Re: ComboBox SelectionChangedCommitted + Setting Focus = Problem
Given that the documentation expressly states that application developers should call the Select method to set focus to a control and NOT the Focus method, the very first thing you should do is change that and see whether that fixes the problem. Maybe it will and maybe it won't but if you do exactly what the documentation tells you not to do and something doesn't work, the first change to make is obvious.
Re: ComboBox SelectionChangedCommitted + Setting Focus = Problem
Edited....It doesn't work :)
Re: ComboBox SelectionChangedCommitted + Setting Focus = Problem
Thanks JMC
Didn't read the documentation... :(
But changed to Select instead of Focus, and that didn't solve the issue.
Re: ComboBox SelectionChangedCommitted + Setting Focus = Problem
Something I found, maybe it could help:
http://support.microsoft.com/kb/2868238/en-us
EDIT:
After applying the fix this works fine for me:
vb.net Code:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable("Teste")
dt.Columns.Add("ValueColumn", GetType(Integer))
dt.Columns.Add("DisplayColumn", GetType(String))
dt.Rows.Add(0, "111111")
dt.Rows.Add(1, "12341")
dt.Rows.Add(2, "34334")
dt.Rows.Add(3, "55555")
dt.Rows.Add(4, "55566")
dt.Rows.Add(5, "555676")
dt.Rows.Add(6, "88111")
dt.Rows.Add(7, "99911")
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "DisplayColumn"
ComboBox1.ValueMember = "ValueColumn"
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
TextBox1.Text = ComboBox1.Text
'aux = ComboBox1.SelectedIndex 'WorkAround
TextBox1.Focus()
End Sub
Re: ComboBox SelectionChangedCommitted + Setting Focus = Problem
Thanks BlackRiver
That solved the problem! In my specific case was the Enter key, but anyway, problem solved.
Before posting i searched a lot, but looks like i didn't used the right words!
Re: ComboBox SelectionChangedCommitted + Setting Focus = Problem