软键盘应用
触摸一体机没有硬件键盘,系统软键盘占面积大,位于屏幕底部,点击不是很方便。某些时候只需要输入数字,这时弹出九宫格数字键盘就足够了。
以下实现的是弹出一个弹窗作为软键盘。
实现
- 创建一个窗体
FrmSoftKey
,将FormBorderStyle
设置为none
,将startPosition
设置为Manual
- 将一个文本框
textBox1
和需要的按钮拖入,调整样式,如图
- 按钮:
0~9
按钮,.
按钮,删除、清空、退出、确定按钮绑定事件代码如下
public partial class FrmSoftKey : Form
{
public FrmSoftKey()
{
InitializeComponent();
}
public static string input = string.Empty;
public static bool confirm = false;
//这是 0~9 以及 . 按钮绑定的事件
void btn_Click(object sender, EventArgs e)
{
Button B = (Button)sender;
string buttonText = B.Text;
int selectionStart = textBox1.SelectionStart; // 保存当前焦点位置
textBox1.Text = textBox1.Text.Insert(selectionStart, buttonText); // 在焦点位置插入按钮文字
textBox1.SelectionStart = selectionStart + buttonText.Length; // 设置焦点位置
textBox1.Focus(); // 重新设置焦点到文本框
}
//清空按钮
void btn_Clr_Click(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
textBox1.Focus();
}
//删除按钮
void btn_Bsc_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
return;
}
int selectionStart = textBox1.SelectionStart;
textBox1.Text = textBox1.Text.Remove(selectionStart - 1, 1);
textBox1.SelectionStart = selectionStart - 1;
textBox1.Focus();
}
//确定按钮
void btn_Confirm_Click(object sender, EventArgs e)
{
input = textBox1.Text;
confirm = true;
this.Dispose();
this.Close();
}
//退出按钮
private void btn_Esc_Click(object sender, EventArgs e)
{
confirm = false;
this.Dispose();
this.Close();
}
//以下代码是为了获得文本框本来的内容
private void FrmSoftKey_Shown(object sender, EventArgs e)
{
textBox1.Text = input;
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.SelectionLength = 0;
textBox1.Focus();
}
//限制实体键盘只能输入数字和 . ,可以删掉
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// 检查是否是数字或小数点
if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.' && !char.IsControl(e.KeyChar))
{
e.Handled = true; // 取消输入
}
// 确保只能输入一个小数点
if (e.KeyChar == '.' && ((TextBox)sender).Text.Contains('.'))
{
e.Handled = true; // 取消输入
}
}
//回车时触发事件,和点击确定是一样的效果
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// 在这里执行回车键触发的事件逻辑
input = textBox1.Text;
confirm = true;
this.Dispose();
this.Close();
}
}
}
- 至此软键盘窗体已经设计完成,接下来是使用。
将键盘与文本框tb_pwd
绑定(该文本框位于主窗体,弹出的软键盘是个子窗体)。
添加如下Click
事件,将tb_pwd
的点击事件设置为该事件。
温馨提示:如果有很多个文本框的话,不用每个文本框都添加一个Click
事件,Click
事件只要一个就可以了,不同的文本框可以绑定到同一个Click事件。
private void tb_Click(object sender, EventArgs e)
{
TextBox tb_temp = (TextBox)sender;
FrmSoftKey.input = tb_temp.Text;
// 获取 TextBox 的屏幕位置
Point textboxLocation = tb_temp.PointToScreen(Point.Empty);
// 创建一个新的窗体
FrmSoftKey frmSoftKey = new FrmSoftKey();
// 计算窗体的位置
int popupX = textboxLocation.X;
int popupY = textboxLocation.Y;
// 设置窗体的位置
frmSoftKey.Location = new Point(popupX, popupY);
// 显示窗体
frmSoftKey.ShowDialog();
//MessageBox.Show("显示键盘之后");
//MessageBox.Show(FrmSoftKey.input);
if (FrmSoftKey.confirm)
{
tb_temp.Text = FrmSoftKey.input;
tb_temp.SelectionStart = tb_temp.Text.Length;
tb_temp.SelectionLength = 0;
tb_temp.Focus();
}
}
效果
其实和普通的键盘没什么不同,代码不多用一下就知道了。
- 点击文本框之后弹出软键盘
此时软键盘文本框自动聚焦了,既可以点击蓝色按钮进行输入也可以用实体键盘输入。
- 输入完成点击确定
- 再次点击文本框打开软键盘