Okay, this is a really weird error in my windows form.
I have included my test vb class which is a test app. Load it up into Visual Studio.Net and run the app. What is does is populate a combo box with some test data and then it implements some autocomplete code so that as the user types is autocompletes the selection.
Now here's the weird scenario that breaks the form:
- User clicks combo arrow to drop down list of items
- User types the letter 'M'
- Combo Selects 'Martin' option
- User hits enter key
- Combo Selection is still 'Martin'
- User clicks combo arrow to drop down list of items
- User types the letter 'A'
- Combo Selects 'Alan' options
- User hits enter key
- Combo Selection is still 'Alan'
- User clicks submit button
- Combo Selection says that it is 'Martin' What's up with that
Any help that you can offer would be greatly appreciated.
Thanks,
Martin
Imports test Public Class test Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() 'This call is required by the Windows Form Designer. 'Add any initialization after the InitializeComponent() call Dim drOption As DataRow Dim i As Integer = 0 For i = 0 To aIDs.GetLength(0) - 1 cmbTest.DataSource = dtOptions 'Form overrides dispose to clean up the component list. 'Required by the Windows Form Designer 'NOTE: The following procedure is required by the Windows Form Designer <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub #End Region Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click Private Sub cmbTest_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTest.Leave Private Sub cmbTest_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cmbTest.KeyPress If Char.IsControl(e.KeyChar) Then With Me.cmbTest Trace.WriteLine("New Selection: " & cmbTest.SelectedValue) |

Weird Autocomplete Combobox Error
Ast123
If you're using VS 2005, you can use the new AutoCompleteMode to enable system AutoComplete. I'm assuming you're not...
The Windows Forms ComboBox is a wrapper on the Windows OS provided COMBOBOX control (see http://msdn.microsoft.com/library/default.asp url=/library/en-us/shellcc/platform/commctls/comboboxes/aboutcomboboxes.asp). You've turned up a subtle issue with the way the system ComboBox handles the interaction between it's list and edit controls (it doesn't appear to expect the index to be changed while the drop down is popped up). The only work-around I've found is to sync the SelectedIndex when the ComboBox DropDown is popped up. In VS 2005, we've added an event for this but in VS 2003, you'll need to sub-class the ComboBox to get this behavior. The following sample shows how to sub-class the ComboBox to add a DropDownCloseUp event. You can then use this event to sync the SelectedIndex (shown further below).
Public Class ACComboBox
Inherits System.Windows.Forms.ComboBox
Private Const WM_REFLECT_COMMAND As Integer = &H2000 + &H111
Private Const CBN_CLOSEUP As Integer = &H8
Public Event DropDownCloseUp As EventHandler
Private Function HiWord(ByVal val As Integer) As Integer
Return (val >> 16) And &HFFFF
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If (m.Msg = WM_REFLECT_COMMAND) And (HiWord(m.WParam) = CBN_CLOSEUP) Then
RaiseEvent DropDownCloseUp(Me, EventArgs.Empty)
End If
MyBase.WndProc(m)
End Sub
End Class
You're code when need to be modified as follows:
Private Sub cmbTest_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cmbTest.KeyPress
Trace.WriteLine("**Key Press - " & e.KeyChar.ToString())
Trace.WriteLine("Current Selection: " & cmbTest.SelectedValue)
If Char.IsControl(e.KeyChar) Then
Trace.WriteLine("Control Key Pressed")
Return
End If
With Me.cmbTest
Dim ToFind As String = .Text.Substring(0, .SelectionStart) & e.KeyChar
Dim Index As Integer = .FindStringExact(ToFind)
If Index = -1 Then Index = .FindString(ToFind)
If Index = -1 Then Return
.SelectedIndex = Index
.Select(ToFind.Length, .Text.Length - .SelectionStart)
newIndex = Index
e.Handled = True
End With
Trace.WriteLine("New Selection: " & cmbTest.SelectedValue)
Trace.WriteLine("")
End Sub
Private newIndex As Integer
Private Sub cmbTest_DropDownCloseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTest.DropDownCloseUp
If (newIndex <> cmbTest.SelectedIndex) Then
cmbTest.SelectedIndex = newIndex
End If
End Sub
Joe Stegman
The Windows Forms Team
Microsoft Corp.
This posting is provided "AS IS" with no warranties, and confers no rights.