C#winform自定义数字软键盘

软键盘应用

触摸一体机没有硬件键盘,系统软键盘占面积大,位于屏幕底部,点击不是很方便。某些时候只需要输入数字,这时弹出九宫格数字键盘就足够了。
以下实现的是弹出一个弹窗作为软键盘。

实现
  1. 创建一个窗体FrmSoftKey,将FormBorderStyle设置为none,将startPosition设置为Manual
  2. 将一个文本框textBox1和需要的按钮拖入,调整样式,如图
    在这里插入图片描述
  3. 按钮: 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();
        }
    }
}
  1. 至此软键盘窗体已经设计完成,接下来是使用。
    将键盘与文本框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();
    }
}

在这里插入图片描述

效果

其实和普通的键盘没什么不同,代码不多用一下就知道了。

  1. 点击文本框之后弹出软键盘
    此时软键盘文本框自动聚焦了,既可以点击蓝色按钮进行输入也可以用实体键盘输入。
    在这里插入图片描述
    在这里插入图片描述
  2. 输入完成点击确定
    在这里插入图片描述
    在这里插入图片描述
  3. 再次点击文本框打开软键盘
    在这里插入图片描述
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值