TAB单击移动到下一个datagridview单元格但不显示光标

时间:2017-01-30 22:07:24

标签: .net vb.net winforms datagridview

我在Windows窗体中有一个DataGridView,当我按下右箭头键时,光标出现在下一个单元格上。但是当我按下TAB时,选择会改变,但光标不会出现在下一个单元格上,而我真正想要的是让这个光标以某种方式出现。 我认为这将是一个解决方案,当用户在某些ComboBox / TextBox单元格中单击TAB时,可以使用TAB模拟右箭头单击。 我在Windows窗体代码中实际拥有的一些Subs:

Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
    sender.BeginEdit(True)
End Sub

Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
    sender.BeginEdit(True)
End Sub

Private Sub DataGridView1EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    If IsComboBoxCell(DataGridView1.CurrentCell) Then
        Dim cb As ComboBox = TryCast(e.Control, ComboBox)
        If cb IsNot Nothing Then
            cb.DropDownStyle = ComboBoxStyle.DropDown
            cb.DropDownHeight = 200
            cb.AutoCompleteSource = Windows.Forms.AutoCompleteSource.ListItems
            cb.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend
            RemoveHandler cb.Validated, AddressOf cb_Validated
            AddHandler cb.Validated, AddressOf cb_Validated
        End If
    End If
End Sub

Private Sub cb_Validated(sender As Object, e As EventArgs)
    Dim selectedItem = CType(sender, ComboBox).SelectedItem
    Dim col = CType(DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex), DataGridViewComboBoxColumn)
    If String.IsNullOrEmpty(col.ValueMember) Then
        DataGridView1.CurrentCell.Value = selectedItem

    End If
End Sub

它适用于Enter但不适用于TAB。 有可能吗?用户按下TAB,但它会像右箭头或其他解决方案一样触发?

1 个答案:

答案 0 :(得分:1)

检查您的" StandardTab"属性未设置为" true"

如果它不起作用(我不知道为什么)尝试处理密钥

 private void Data_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Tab)
            SendKeys.Send("{RIGHT}");
    }

对于c#代码的sory,但我想你很容易在vb中写它