在重点关注的文本框和DataGridView单元格上使用虚拟键盘 [英] Use a virtual Keyboard on focused Textboxes and DataGridView Cells

查看:79
本文介绍了在重点关注的文本框和DataGridView单元格上使用虚拟键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表单中,我有多个文本框,这些文本框是使用通过按钮创建的In in Form键盘写入的.我在Form.Load中有这段代码,它使用事件处理程序来确定哪个Textbox具有Focus:

 对于每个控件作为Me.Controls中的控件如果control.GetType.Equals(GetType(TextBox))然后暗淡的textBox为TextBox =控件AddHandler textBox.Enter,Sub()FocussedTextbox = textBox万一下一个 

然后我在每个按钮上使用它来写一个特定的字符:

  Private Sub btnQ_Click(发送者为对象,e作为EventArgs)处理btnQ.Click如果btnQ.Text ="Q",则返回"n".然后FocussedTextbox.Text + ="Q";ElseIf btnQ.Text ="q"然后FocussedTextbox.Text + ="q";万一结束子 

到目前为止,我很好,一切都按预期进行.问题是我还有一个要写入的 DataGridView ,但不能像在文本框上那样专注于所选的单元格.

我尝试过:

 对于每个控件作为Me.Controls中的控件如果control.GetType.Equals(GetType(TextBox))然后暗淡的textBox为TextBox =控件AddHandler textBox.Enter,Sub()FocussedTextbox = textBoxElseIf control.GetType.Equals(GetType(DataGridViewCell))然后DGVC昏暗为DataGridView =控件AddHandler DGVC.CellBeginEdit,Sub()FocussedTextbox = DGVC万一下一个 

但是它只是选择了我的上一个文本框.

我将变量 FocussedTextbox 声明为 Control ,因此它不是特定于Textbox而是任何控件.

任何帮助将不胜感激.

解决方案

要将文本添加到当前的

In my Form I have various Textboxes that I write into with an in Form keyboard I created using Buttons. I have this code in Form.Load, which uses an event handler to determine which Textbox has the Focus:

For Each control As Control In Me.Controls
    If control.GetType.Equals(GetType(TextBox)) Then
        Dim textBox As TextBox = control
        AddHandler textBox.Enter, Sub() FocussedTextbox = textBox
     End If
Next

Then I use this on each button to write a specific character:

Private Sub btnQ_Click(sender As Object, e As EventArgs) Handles btnQ.Click
    If btnQ.Text = "Q" Then
        FocussedTextbox.Text += "Q"
    ElseIf btnQ.Text = "q" Then
        FocussedTextbox.Text += "q"
    End If
End Sub

Up to that point I'm good and everything works as intended. The problem is I also have a DataGridView I want to write into but can't focus on it selected cells as I do on Textboxes.

I tried this:

For Each control As Control In Me.Controls
    If control.GetType.Equals(GetType(TextBox)) Then
        Dim textBox As TextBox = control
        AddHandler textBox.Enter, Sub() FocussedTextbox = textBox
    ElseIf control.GetType.Equals(GetType(DataGridViewCell)) Then
        Dim DGVC As DataGridView = control
        AddHandler DGVC.CellBeginEdit, Sub() FocussedTextbox = DGVC
    End If
Next

But it just selects my last Textbox.

I declared the variable FocussedTextbox as Control so it's not specific to Textbox but any control.

Any help will be greatly appreciated.

解决方案

To add text to the current ActiveControl using Buttons, these Button must not steal the focus from the ActiveControl (otherwise they become the ActiveControl).
This way, you can also avoid all those FocusedTextbox = textBox etc. and remove that code.

You just need Buttons that don't have the Selectable attribute set. You can use a Custom Control derived from Button and remove ControlStyles.Selectable in its constructor using the SetStyle method:

Public Class ButtonNoSel
    Inherits Button
    Public Sub New()
        SetStyle(ControlStyles.Selectable, False)
    End Sub
End Class

Replace your Buttons with this one (or, well, just set the Style if you're already using Custom Controls).


To replace the existing Buttons with this Custom Control:

  • Add a new class object to your Project, name it ButtonNoSel, copy all the code above inside the new class to replace the two lines of code you find there.
  • Build the Project. You can find the ButtonNoSel Control in your ToolBox now. Replace your Buttons with this one.
  • Or, open up the Form's Designer file and replace (CTRL+H) all System.Windows.Forms.Button() related to the Virtual KeyBoard with ButtonNoSel.
  • Remove the existing event handlers, these are not needed anymore.

Add the same Click event handler in the Constructor of the class that hosts those Buttons (a Form or whatever else you're using).
You can then remove all those event handlers, one for each control, that you have now; only one event handler is needed for all:

Public Sub New()
    InitializeComponent()

    For Each ctrl As Control In Me.Controls.OfType(Of ButtonNoSel)
        AddHandler ctrl.Click, AddressOf KeyBoardButtons_Click
    Next
End Sub

Of course, you also don't need to add event handlers to any other control, this is all that's required.

Now, you can filter the Control types you want your keyboard to work on, e.g., TextBoxBase Controls (TextBox and RichTextBox), DataGridView, NumericUpDown etc.
Or filter only special cases that need special treatment (e.g., MonthCalendar).

To add the char corresponding to the Button pressed, you can use SendKeys.Send(): it will insert the new char in the current insertion point, so you don't need any other code to store and reset the caret/cursor position as it happens if you set the Text property of a Control.

In this example, I'm checking whether the ActiveControl is a TextBoxBase Control, then just send the char that the clicked Button holds.
If it's a DataGridView, first send F2 to enter Edit Mode, then send the char.
You could also just send a char (so, no filter would be required), but in this case, you'll replace, not add to, the existing value of that Cell.

Private Sub KeyBoardButtons_Click(sender As Object, e As EventArgs)
    Dim selectedButton = DirectCast(sender, Control)
    Dim keysToSend = String.Empty

    If TypeOf ActiveControl Is TextBoxBase Then
        keysToSend = selectedButton.Text
    ElseIf TypeOf ActiveControl Is DataGridView Then
        Dim ctrl = DirectCast(ActiveControl, DataGridView)
        If TypeOf ctrl.CurrentCell IsNot DataGridViewTextBoxCell Then Return
        SendKeys.Send("{F2}")
        keysToSend = selectedButton.Text
    Else
        ' Whatever else
    End If
    If Not String.IsNullOrEmpty(keysToSend) Then
        SendKeys.Send(keysToSend)
    End If
End Sub

► Note that {F2} is sent just once: when the Cell enters Edit Mode, the ActiveControl is a DataGridViewTextBoxEditingControl, hence a TextBox Control, handled by the TextBoxBase filter.

This is how it works (using just the code posted here):

这篇关于在重点关注的文本框和DataGridView单元格上使用虚拟键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆