
一个最近的项目开发的触摸屏应用程序,因为没有键盘,所以都需要使用软键盘. 尽管可以调用系统的软键盘winform 触摸屏软键盘,但无法将系统软件磁盘固定在某个位置,即,每次弹出的位置都不固定,甚至无法阻止需要输入下一个文本框的时间. 其次,该程序在屏幕上全屏显示. 用户无法退出该程序,但是在调用系统软件磁盘后,您可以轻松关闭该程序,或者关闭并重新启动计算机. 因此,您自己编写一个小的键盘程序,因为只需要输入一个接口,因此键盘直接嵌入到窗体中.

首先看一下测试效果图

有四个文本框可以接受用户输入. 当用户单击输入框时,他可以单击按钮以输入相应的数字,而在单击退格键时winform 触摸屏软键盘,则要逐一删除输入内容. 同时,如果用户发现输入错误
您可以根据输入内容在指定位置添加或删除.
具体代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace KeyBoard
{
public partial class Form1 : Form
{
TextBox currentTextBox;
int currentpos;
public Form1()
{
InitializeComponent();
foreach (Control ctl in Controls)
{
if (ctl.GetType().Equals(typeof(TextBox)))
ctl.LostFocus += new EventHandler(ctl_LostFocus);
else if (ctl.Name == "btnKeyBoardClear")
ctl.Click += new EventHandler(ctl_ClearClick);
else if (ctl.GetType().Equals(typeof(Button)) && ctl.Name != "btnSubmit")
ctl.Click += new EventHandler(ctl_Click);
else
ctl.Enter += new EventHandler(ctl_Enter);
}
}
void ctl_ClearClick(object sender, EventArgs e)
{
if (currentTextBox != null)
{
try
{
currentTextBox.Text = currentTextBox.Text.Remove(currentpos - 1, 1);
currentpos -= ((Button)sender).Text.Length;
currentTextBox.Focus();
currentTextBox.SelectionStart = currentpos + 1;
currentTextBox.SelectionLength = 0;
}
catch
{
MessageBox.Show("请从正确位置删除!");
}
}
else
return;
}
void ctl_Enter(object sender, EventArgs e)
{
currentTextBox = null;
}
void ctl_Click(object sender, EventArgs e)
{
if (currentTextBox != null)
{
currentTextBox.Text = currentTextBox.Text.Insert(currentpos, ((Button)sender).Text);
currentpos += ((Button)sender).Text.Length;
currentTextBox.Focus();
currentTextBox.SelectionStart = currentpos;
currentTextBox.SelectionLength = 0;
}
else
return;
}
void ctl_LostFocus(object sender, EventArgs e)
{
currentTextBox = (TextBox)sender;
currentpos = currentTextBox.SelectionStart;
}
}
}
winform数字软键盘
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/shouji/article-172172-1.html
别人会给你加息么