I have a ComboBox. My issue is when I click on element 1, then start typing the name of a element 2, the first letter of my element 2 gets added. This only happens on the key down after a click event.
Process:
- Click on ComboBox
- Type in a name
- Click on the elements name - element gets added to my grid
- Type in another name - ISSUE - first element that matches the first letter I input gets added to the grid
Clicking and the enter key on a single selected element works fine.
My AutoCompleteMode is set to Append and AutoCompleteSource is ListItems
The solution I thought to control a click is in my SelectionChangeComitted
function. If I can control the flag isCLick
to only be set true when we know that a element from the combo box is clicked by the mouse. MouseClick
will return true if the user clicks on the overall ComboBox therefore will always be true if we're dealing with this particular ComboBox. Since the parameter is EventArgs
I cannot directly cast it to MouseEventArgs
, would be great if I could.
To sum up what my problem is: I need to replace isClick
boolean in my SelectionChangeCommitted
to validate if the SelectionChangeCommitted
was triggered by a click or not.
Is that possible in this case? Would there be any alternatives to bypass this issue?
I have event handler code below, the sequence of the call is: MouseClick -> PreviewKeyDown -> SelectionChangeCommitted -> SelectedValueChanged
AddHandler CType(ctl, ComboBox).PreviewKeyDown, Sub(sender As Object, e As PreviewKeyDownEventArgs)
If e.KeyCode = Keys.Down OrElse e.KeyCode = Keys.Up OrElse e.KeyCode = Keys.Right OrElse e.KeyCode = Keys.Left Then
e.IsInputKey = False
AddToGrid = False
isArrowKey = True
isClick = False
ElseIf e.KeyCode = Keys.Enter OrElse e.KeyCode = Keys.Tab Then
AddToGrid = True
isArrowKey = False
isClick = False
'Check if row already exists before insert a new one.
If Not RowExists(DirectCast(ctl, Control).Tag, DirectCast(ctl, ComboBox).Text.ToUpper()) Then AppendGrid(sender)
Else
AddToGrid = False
isArrowKey = False
isClick = True
End If
End Sub
AddHandler CType(ctl, ComboBox).SelectedValueChanged, Sub(sender As Object, e As EventArgs)
'MessageBox.Show("SelectionValueChanged")
If CType(sender, ComboBox).SelectedValue Is Nothing Then Return
If Not AddToGrid Then Return
'Check if row already exists before insert a new one.
If Not RowExists(DirectCast(ctl, Control).Tag, DirectCast(ctl, ComboBox).SelectedValue.ToString().ToUpper()) Then
AppendGrid(sender)
End If
End Sub
AddHandler CType(ctl, ComboBox).SelectionChangeCommitted, Sub(sender As Object, e As EventArgs)
'MessageBox.Show("SelectionChangedCommitted")
If CType(sender, ComboBox).SelectedValue Is Nothing Then Return
If isArrowKey Then Return 'if an arrow was used, then don't add it to the grid
If isClick Then
AddToGrid = True
End If
'Check if row already exists before insert a new one.
If Not RowExists(DirectCast(ctl, Control).Tag, DirectCast(ctl, ComboBox).SelectedValue.ToString().ToUpper()) AndAlso AddToGrid Then
AppendGrid(sender)
End If
End Sub
AddHandler CType(ctl, ComboBox).MouseClick, Sub(sender As Object, e As MouseEventArgs)
'MessageBox.Show("MouseClick")
isClick = True
AddToGrid = True
End Sub