I am trying to resolve a problem with keyboard handling for a Windows Forms application that consists of a number of datagrids. Adding and deleting records is allowed only with button actions. The datagrids contain combobox columns. Column navigation is by TAB/Shift-TAB and RIGHT/LEFT Arrows. I am trying to achieve keyboard control for the combobox columns as follows:
Press ENTER for first time in the combobox column drops the list (droppeddown=true), UP/DOWN Arrows navigates the list, press ENTER again closes the drop (droppeddown=false) and selects the value. I am able to get the combobox to drop on the first ENTER press, navigate UP/DOWN the list, but am having trouble with the second ENTER.
I have tried multiple configurations using variously KeyPress, KeyDown and KeyUp in my comboboxcolumn. As well I have added code to the combobox trapping various messages. Any help would be appreciated.
Here is a version of code for both the ComboBoxColumn and ComboBox that kind of works:
ComboBox Code:
ComboBoxColumn Code:
Press ENTER for first time in the combobox column drops the list (droppeddown=true), UP/DOWN Arrows navigates the list, press ENTER again closes the drop (droppeddown=false) and selects the value. I am able to get the combobox to drop on the first ENTER press, navigate UP/DOWN the list, but am having trouble with the second ENTER.
I have tried multiple configurations using variously KeyPress, KeyDown and KeyUp in my comboboxcolumn. As well I have added code to the combobox trapping various messages. Any help would be appreciated.
Here is a version of code for both the ComboBoxColumn and ComboBox that kind of works:
ComboBox Code:
Code:
Public Class NoKeyUpCombo
Inherits CompletionCombo
Private Const WM_KEYUP As Integer = 257
Protected Overloads Overrides Function ProcessKeyMessage(ByRef m As Message) As Boolean
Dim key As Keys = CType(CType(m.WParam.ToInt32, Integer), Keys)
Select Case key
Case Keys.Tab
Return True
Case Keys.Up, Keys.Down
Return False
Case Else
Return MyBase.ProcessKeyMessage(m)
End Select
End Function
Protected Overloads Overrides Function IsInputKey(ByVal key As Keys) As Boolean
Select Case key
Case Keys.Up, Keys.Down
Return True
End Select
Return MyBase.IsInputKey(key)
End Function
ComboBoxColumn Code:
Code:
Private _ColumnComboBox As NoKeyUpCombo
AddHandler _ColumnComboBox.KeyUp, AddressOf ComboBoxKeyUp
AddHandler _ColumnComboBox.KeyDown, AddressOf ComboBoxKeyDown
Private Sub ComboBoxKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Execute Or e.KeyCode = Keys.Return Then
Dim cbx As NoKeyUpCombo = CType(sender, NoKeyUpCombo)
cbx.SelectedIndex = _SelectedIndex
Debug.WriteLine(_ColumnComboBox.SelectedIndex.ToString & "KeyDown-ENTER:" & _ColumnComboBox.Text)
e.Handled = False
End If
End Sub
Private Sub ComboBoxKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
Dim i As Integer
If Not _ColumnComboBox.DroppedDown Then
If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Execute Or e.KeyCode = Keys.Return Then
_ColumnComboBox.DroppedDown = True
KeyStrokeProcessed = True
_ColumnComboBox.Invalidate()
_ColumnComboBox.Update()
End If
Else
Select Case e.KeyCode
Case Keys.Down
i = _ColumnComboBox.SelectedIndex
If _ColumnComboBox.Items.Count > i Then
_SelectedIndex = i + 1
_ColumnComboBox.SelectedIndex = _SelectedIndex
_ColumnComboBox.Invalidate()
_ColumnComboBox.Update()
e.Handled = False
Debug.WriteLine(_ColumnComboBox.SelectedIndex.ToString & "-Down:" & _ColumnComboBox.Text)
End If
Case Keys.Up
i = _ColumnComboBox.SelectedIndex
If i > 0 Then
_SelectedIndex = i - 1
_ColumnComboBox.SelectedIndex = _SelectedIndex
_ColumnComboBox.Invalidate()
_ColumnComboBox.Update()
e.Handled = False
Debug.WriteLine(_ColumnComboBox.SelectedIndex.ToString & "-Up:" & _ColumnComboBox.Text)
End If
Case Keys.Enter, Keys.Execute, Keys.Return
If _ColumnComboBox.DroppedDown Then
_ColumnComboBox.SelectedIndex = _SelectedIndex
Debug.WriteLine(_SelectedIndex.ToString & ":*" & _ColumnComboBox.Text)
_ColumnComboBox.DroppedDown = False
KeyStrokeProcessed = False
_ColumnComboBox.Invalidate()
_ColumnComboBox.Update()
'e.Handled = True
Debug.WriteLine(_ColumnComboBox.SelectedIndex.ToString & "ENTER Press 2-New Value:" & _ColumnComboBox.Text)
End If
End Select
'Dim cbx As NoKeyUpCombo = CType(sender, NoKeyUpCombo)
'cbx.SelectedIndex = _SelectedIndex
' Debug.WriteLine(_ColumnComboBox.SelectedIndex.ToString & "-dgcKeyPress:" & _ColumnComboBox.Text)
Return
End If
End Sub