在窗体数字键盘 C#

In form numberpad C#

我想在我的表单中添加一个表单数字键盘,使程序更易于触摸。我在这个表单上有多个文本框,当按下回车键时它们会改变焦点。

我尝试了 SendKeys.Send("#"),但是当我点击按钮时,它只是将焦点转移到按钮上,而在我试图输入的文本框中什么也没做。

有这方面的指南吗?我已经看过,但我能找到的都是屏幕键盘,它们可以在表单外使用,但不能在表单内使用。

扩展一下 Hans Passant 的想法,您可以以此为起点。

表格

在您的表单中添加您需要的文本框和一个 PictureBox。图片框将包含一张图片,其中包含您的用户需要能够键入的字符。

设置 属性 Image to an image file(浏览)(或创建您自己的)
设置 属性 SizeMode to AutoSize 以显示完整图片。

接下来转到事件并为 MouseClick 添加一个事件处理程序。

代码

将以下代码添加到处理程序中:

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
    // how many rows and columns do we have
    // in the picture used
    const int maxrows = 4;
    const int maxcols = 3;

    // based on each position (numbered from left to right, top to bottom)
    // what character do we want to add the textbox 
    var chars = new [] {'1','2','3','4','5','6','7','8','9', '+', '0', '-'};
       
    // on which row and col is clicked, based on the mouse event
    // which hold the X and Y value of the coordinates relative to 
    // the control.
    var  row = (e.Y * maxrows) / this.pictureBox1.Height;
    var col = (e.X * maxcols) / this.pictureBox1.Width;
       
    // calculate the position in the char array
    var scancode = row * maxcols + col;
            
    // if the active control is a TextBox ...
    if (this.ActiveControl is TextBox)
    {
        // ... add the char to the Text.
        // add error and bounds checking as well as 
        // handling of special chars like delete/backspace/left/right
        // if added and needed
        this.ActiveControl.Text += chars[scancode];
    }
}

我认为代码是不言自明的。请记住,这里没有进行任何错误检查。

结果

这是最终结果的样子: