检测 Datagridview 控件中的字母或数字键并将处理后的键发送到文本框 VB.BET

发布于 01-02 18:11 字数 113 浏览 4 评论 0原文

我正在开发一个包含 datagridview 和文本框的表单,我需要 datagridview 检测字母表中的任何字母或数字,然后选择输入并将按下的键发送到输入。

我找不到任何解决方案,提前致谢。

I'm working on a Form that contains a datagridview and textbox, I need the datagridview detect any letter of the alphabet or numbers, then select the input and send the key pressed to input.

I can not find any solution for this, thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗2025-01-09 18:11:55

您需要将事件处理程序附加到正在接收响应 KeyPress 事件的数据的单元格编辑控件。这可以通过处理 EditingControlShowing 事件来完成。

以下是执行此操作的一些基本代码:

Public Class Form1

    Private Sub DataGridView1_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        Dim c As Control
        c = e.Control

        AddHandler c.KeyPress, AddressOf Handle_KeyPress


    End Sub

    Protected Sub Handle_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Char.IsLetterOrDigit(e.KeyChar) Then
            TextBox1.Text += e.KeyChar
            e.Handled = True
        End If
    End Sub
End Class

您还可以响应其他事件,例如 KeyDown,但通常首选 KeyPress,因为它为您提供带有事件参数的 Char。对于像 KeyDown 这样的事件,您将拥有 KeyCodes,它不允许您轻松判断输入是大写还是小写。

You will need to attach an event handler to the cell editing control that is receiving the data that will respond to the KeyPress event. This can be done by handling the EditingControlShowing event.

Here is some basic code that does this:

Public Class Form1

    Private Sub DataGridView1_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        Dim c As Control
        c = e.Control

        AddHandler c.KeyPress, AddressOf Handle_KeyPress


    End Sub

    Protected Sub Handle_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Char.IsLetterOrDigit(e.KeyChar) Then
            TextBox1.Text += e.KeyChar
            e.Handled = True
        End If
    End Sub
End Class

There are other events that you can respond to such as KeyDown but KeyPress is generally preferred since it gives you a Char with the event args. With events like KeyDown you will have KeyCodes instead, which don't allow you to easily tell if the input was upper or lower case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文