在形式上numberpad C# [英] In form numberpad C#

查看:309
本文介绍了在形式上numberpad C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在形式上numberpad添加到我的形式使程序更加友好的触控。我有这种形式在多个文本框更改当输入键被按下的焦点。

I would like to add an in form numberpad to my form to make the program more touch friendly. I have multiple text boxes on this form that change focus when the enter key is pressed.

我试过SendKeys.Send(#),但是当我按一下按钮它只是改变焦点的按钮,并执行文本框,我想输入什么都不里面

I tried SendKeys.Send("#"), but when I click the button it just changes focus to the button and does nothing inside the text box I was trying to type in.

有一个指南或东西呢?我期待和所有我能找到一些对形式以外工作的屏幕键盘,而不是里面。

Is there a guide or something for this? I have looked and all I can find are on screen keyboards that work outside of the form, but not inside.

推荐答案

来扩展从汉斯帕桑特的想法一点,你可以用这个作为一个起点。

Expanding a little bit on the idea from Hans Passant you could use this as a starting point.

在您的表单中添加您需要的文本框和一个的图片框。该图片将与你的用户需要能够键入的字符图像。

In your form add the textboxes you need and a PictureBox. The picturebox will have an image with the characters your users need to be able to type.

设置属性的 图片 到的图像文件(浏览吧)(或创建你自己的)结果
设置属性的 SizeMode 自动调整大小 ,以便完整的图片所示。

Set the property Image to an image file (browse for it) (or create your own)
Set the property SizeMode to AutoSize so the complete picture is shown.

接下来转到事件并添加事件处理程序进行的 鼠标点击

Next goto the events and add an eventhandler for MouseClick.

下面的代码添加到处理程序:

Add the following code to the handler:

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];
    }
}



中的代码是我认为自我解释。请记住,没有错误检查任何正在这里完成。

The code is I think self explanatory. Keep in mind that no error checking whatsoever is being done here.

这是最终的结果将是什么样子:

This is what the end result will look like:

这篇关于在形式上numberpad C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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