Results 1 to 7 of 7

Thread: ComboBox SelectionChangedCommitted + Setting Focus = Problem

  1. #1

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Question 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:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    4.         Dim dt As New DataTable("Teste")
    5.         dt.Columns.Add(New DataColumn("TestColumn"))
    6.         dt.Rows.Add({"11111"})
    7.         dt.Rows.Add({"12341"})
    8.         dt.Rows.Add({"34334"})
    9.         dt.Rows.Add({"55555"})
    10.         dt.Rows.Add({"55566"})
    11.         dt.Rows.Add({"88111"})
    12.         dt.Rows.Add({"99911"})
    13.  
    14.         With ComboBox1
    15.             .DropDownStyle = ComboBoxStyle.DropDownList
    16.             .AutoCompleteMode = AutoCompleteMode.SuggestAppend
    17.             .DisplayMember = "TestColumn"
    18.             .ValueMember = "TestColumn"
    19.             .DataSource = dt
    20.         End With
    21.     End Sub
    22.  
    23.    
    24.  
    25.     Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
    26.         TextBox1.Text = ComboBox1.SelectedValue.ToString
    27.         'aux = ComboBox1.SelectedIndex 'WorkAround
    28.         TextBox1.Focus()
    29.     End Sub
    30.  
    31.     ''WorkAround
    32.     'Private aux As Integer = -1
    33.     'Private Sub TextBox1_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus
    34.     '    ComboBox1.SelectedIndex = aux
    35.     'End Sub
    36. 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...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: ComboBox SelectionChangedCommitted + Setting Focus = Problem

    Edited....It doesn't work
    Last edited by BlackRiver; Feb 5th, 2014 at 07:09 AM.

  4. #4

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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.

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  5. #5
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    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:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    4.         Dim dt As New DataTable("Teste")
    5.  
    6.         dt.Columns.Add("ValueColumn", GetType(Integer))
    7.         dt.Columns.Add("DisplayColumn", GetType(String))
    8.  
    9.         dt.Rows.Add(0, "111111")
    10.         dt.Rows.Add(1, "12341")
    11.         dt.Rows.Add(2, "34334")
    12.         dt.Rows.Add(3, "55555")
    13.         dt.Rows.Add(4, "55566")
    14.         dt.Rows.Add(5, "555676")
    15.         dt.Rows.Add(6, "88111")
    16.         dt.Rows.Add(7, "99911")
    17.  
    18.  
    19.         ComboBox1.DataSource = dt
    20.         ComboBox1.DisplayMember = "DisplayColumn"
    21.         ComboBox1.ValueMember = "ValueColumn"
    22.  
    23.         ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
    24.         ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
    25.         ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    26.  
    27.  
    28.  
    29.     End Sub
    30.  
    31.  
    32.  
    33.  
    34.  
    35.  
    36.     Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
    37.         TextBox1.Text = ComboBox1.Text
    38.  
    39.         'aux = ComboBox1.SelectedIndex 'WorkAround
    40.         TextBox1.Focus()
    41.     End Sub
    Last edited by BlackRiver; Feb 5th, 2014 at 07:50 AM.

  6. #6

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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!

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  7. #7
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: ComboBox SelectionChangedCommitted + Setting Focus = Problem

    Glad I could help

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width