zoukankan      html  css  js  c++  java
  • Implement Overtype Mode in MaskedTextBox

    The button "Insert" on the PC's keyboard can switch the input mode, insert and overtype. Here implement the mode "overtype" in MaskedTextBox.

    private void Form1_Load(object sender, EventArgs e)
    {
        maskedTextBox1.Mask = "00/00/0000";
        maskedTextBox1.Text = "12/21/3321";
    }
    
    private void maskedTextBox1_KeyUp(object sender, KeyEventArgs e)
    {
    
        string character = e.KeyData.ToString();
        //if (e.KeyData == Keys.D0 || e.KeyData == Keys.D1 || e.KeyData == Keys.D2 || e.KeyData == Keys.D3 || e.KeyData == Keys.D4
        //    || e.KeyData == Keys.D5 || e.KeyData == Keys.D6 || e.KeyData == Keys.D7 || e.KeyData == Keys.D8 || e.KeyData == Keys.D9
        //    || e.KeyData == Keys.NumPad0 || e.KeyData == Keys.NumPad1 || e.KeyData == Keys.NumPad2 || e.KeyData == Keys.NumPad3
        //    || e.KeyData == Keys.NumPad4 || e.KeyData == Keys.NumPad5 || e.KeyData == Keys.NumPad6 || e.KeyData == Keys.NumPad7
        //    || e.KeyData == Keys.NumPad8 || e.KeyData == Keys.NumPad9)
        //{
        if (e.KeyValue < 58 && e.KeyValue > 47 || e.KeyValue < 106 && e.KeyValue > 95)
        {
            int index1 = maskedTextBox1.SelectionStart; // cursor position
            if (index1 < maskedTextBox1.Text.Length)
            {
                string modified = maskedTextBox1.Text.Remove(index1, 1);
                maskedTextBox1.Text = modified.Insert(index1, character);
                if (index1 < maskedTextBox1.Text.Length - 1 && maskedTextBox1.Text[index1 + 1] == '/')
                {
                    maskedTextBox1.SelectionStart = index1 + 2;
                }
                else
                {
                    maskedTextBox1.SelectionStart = index1 + 1;
                }
            }
        }
        //}
    }

    When using the Chinese input method, there will be a case where KeyData is always "ProcessKey" and KeyValue is always 229.

    In order to solve it, we can disable Chinese input by disabling IME.

    maskedTextBox1.ImeMode = ImeMode.Disable;
  • 相关阅读:
    jenkins X实践系列(2) —— 基于jx的DevOps实践
    K8S集群安装
    google gcr.io、k8s.gcr.io 国内镜像
    使用.NET Core+Docker 开发微服务
    APM 原理与框架选型
    统一配置中心选型对比
    【开源小软件 】Bing每日壁纸 V1.2.1
    【开源小软件 】Bing每日壁纸 让桌面壁纸保持更新
    互联网企业级监控系统 OpenFalcon
    完整的房间类游戏解决方案AiJ
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/9958698.html
Copyright © 2011-2022 走看看