ComboBox KeyDown and AutoComplete..

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Okay,

I designed this a while ago and it appeared to work then, but something has changed and I'm not exactly sure what.

In effect I have a ComboBox Hovering over a ListView, and is moved/redrawn over the "SubItems" (column 1 of the listview) for editing purposes. The presentation is quite spectactular (imo) but I'm getting a weird glitch.

Before I was using arrays as the the DataSource of the ComboBox, but due to further complications of the application I've changed it to datatables with appropriate Display/Value members.
The setup is simple:

(This is Pseudo code in case that isn't obvious)
VB.NET:
ListView_SelectedItemChanged() {
   if e.item.Group = LV.Group(0) then
     ItemCmb.DataSource = DS1
   else
     ItemCmb.Datasource = DS2
   end if
   ItemCmb.Location = MovedLocationFromSubItem()
   ItemCmb.Focus() ' <-- the important line!!!
}
So yea, at the end of my selection change in the "ListView" i return the focus to the ComboBox.
VB.NET:
   Private Sub ItemCmb_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ItemCmb.KeyDown
      If Not ItemCmb.DroppedDown Then
         If (e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Down) AndAlso (Not (e.Shift OrElse e.Alt OrElse e.Control)) Then
            e.SuppressKeyPress = True
            lvImpMap.Select()
            lvImpMap.Focus()
            SendKeys.SendWait(IIf(e.KeyCode = Keys.Up, "{UP}", "{DOWN}"))
         End If
      End If
   End Sub
THe Idea here was simple, that when the ComboBox is Not in a currently DropDown-ed state, the Arrow Keys (up/down) are passed back up to the ListView control, which then goes to the next item, moves the ComboBox and returns focus to it.

However, I also have "AutoCompleteMode = SuggestAppend" and "AutoCompleteSource = ListItems". Before I don't think this happened, but it is happening now, that when I type the first character of an item in the list, there are often more than one item with that as the first letter.
  • Code
  • Name
  • City
  • State
  • Street
  • Zip
Given the above, i type "c" and the drop down appears listing "City" and "Code" with the "ity" of city highlighted in the box. I press Enter and my ItemCmb_SelectedValueChanged() event Triggers, setting the value of the ListView.Items.Subitem that is currently being edited.

So far so good, it is handling as it should. But now, I press down/up arrow, and the combobox reverts to thinking I'm still looking for a Suggested value or whatnot and my KeyDown event for the ComboBox is no longer being triggered. This behavior persists, forcing me to use the mouse to select the next item in the listview (or tabbing in and out of the control to move into the listview) but even on other items that behavior maintains, the up/down returning to the "SuggestAppend" "History" or something. But when I delete the suggestappend text from being entered and select an item from the drown down with the mouse or arrow keys, the keydown event works again and my old (desired) behavior resumes.

How can the control currently effect my KeyDown Event to not be processed once a SuggestAppend autocomplete mode has been triggered, and how might I eliminate that behavior upon pressing the Enter Key. in the control, etc, etc, etc.

Thanks :)
 
After pressing enter and selecting your item in the combox is it moving the focus to the listview where you want it?
 
Not exactly. the design is basically to provide an input for the subitem columns of the listview. I ended up resolving the situation by changing the autocomplete parameters once a selection had been made.
VB.NET:
 Private Sub ItemCmb_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ItemCmb.SelectedValueChanged
      If (Not _initcmb) AndAlso (lvImpMap.SelectedItems.Count = 1) AndAlso (ItemCmb.SelectedIndex > -1) Then
         Dim drv As DataRowView = ItemCmb.SelectedItem
         ItemCmb.AutoCompleteMode = AutoCompleteMode.None
         If drv IsNot Nothing Then
            With lvImpMap.SelectedItems(0).SubItems(1)
               .Text = ItemCmb.Text
               .Tag = drv.Row
            End With
         End If
         ItemCmb.AutoCompleteMode = AutoCompleteMode.SuggestAppend
      End If
   End Sub
by turning the autocompletemode off then back on again it cleared its internal buffer and the next keypress acted normally. the combobox retains the focus all the time, except when navigating, it hands the control back to the listview, transmits the appropriate key (up/down) and when the listview changes items, it reactivates the combo box again. so the user never actually "sees" the listview retaining the focus.
 
I re-read your original post and stand corrected; I miss understood your original post.

I thought the problem was that when trying to use the up/down keys in the listview, it was unintentially changing items in the combobox.
 
Back
Top