zoukankan      html  css  js  c++  java
  • WPF TextBox 一些设置技巧

    WPF TextBox 一些设置技巧

    运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-01

    参考:

    章节:

    1. 取消输入法
    2. 输入方式设定为Overwrite
    3. 限定输入字符数

    一、取消输入法

    TextBox txbx=new TextBox();
    InputMethod.SetIsInputMethodEnabled(txbx, false);//关掉输入法
    

    二、输入方式设定为Overwrite

    //把输入改成 overwrite 模式
    // fetch TextEditor from myTextBox
    TextBox txbx=new TextBox();
    PropertyInfo textEditorProperty = typeof(TextBox).GetProperty("TextEditor", BindingFlags.NonPublic | BindingFlags.Instance);
    object textEditor = textEditorProperty.GetValue(txbx, null);
    // set _OvertypeMode on the TextEditor
    PropertyInfo overtypeModeProperty = textEditor.GetType().GetProperty("_OvertypeMode", BindingFlags.NonPublic | BindingFlags.Instance);
    overtypeModeProperty.SetValue(textEditor, true, null);
    
    

    三、限定输入字符数

    在 KeyDown 事件里利用 SelectionStart 来设定,下面例子是限定2个字符

    private void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
            TextBox txbx = sender as TextBox;
            //只能有两个字符
            if (txbx.SelectionStart < 2)
            {
                e.Handled = false;//false才能通过
            }
            else
            {
                e.Handled = true;
            }
    }
    
  • 相关阅读:
    获得随机数
    Android Studio中的神操作
    Android Studio中的神操作
    我的github首页
    我的github首页
    初步尝试kotlin
    初步尝试kotlin
    创建自己的github博客
    js方法重载
    【HPU】[1014]【C语言训练】亲密数
  • 原文地址:https://www.cnblogs.com/leemano/p/10799542.html
Copyright © 2011-2022 走看看