• WPF TextBox 只能输入数字键


     <Grid>
            <TextBox Name="textBox1" PreviewTextInput="textBox1_PreviewTextInput" 
                     HorizontalAlignment="Stretch"   VerticalAlignment="Center"   />
        </Grid>
    1
    2
    3
    4
    5
    6
            //using System.Text.RegularExpressions;
            private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
            {
                Regex re = new Regex("[^0-9.-]+");
                e.Handled = re.IsMatch(e.Text);
            }

    KeyDown事件:2011-04-28优化Tab And RightCtrl

    复制代码
    private void tbCount_KeyDown(object sender, KeyEventArgs e)
    {
    TextBox txt = sender as TextBox;

    //屏蔽非法按键
    if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key.ToString() == "Tab")
    {
    if (txt.Text.Contains(".") && e.Key == Key.Decimal)
    {
    e.Handled = true;
    return;
    }
    e.Handled = false;
    }
    else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
    {
    if (txt.Text.Contains(".") && e.Key == Key.OemPeriod)
    {
    e.Handled = true;
    return;
    }
    e.Handled = false;
    }
    else
    {
    e.Handled = true;
    if (e.Key.ToString() != "RightCtrl")
    {
    MessageBox.Show(this.Resources["Txt_InnerPage_ConnPointManage_TabMyConnPoint_AddMyCloudSeeNum_Prompt"].ToString(), this.Resources["Txt_InnerPage_ConnPointManage_TabMyConnPoint_AddMyCloudSeeNum_PromptTitle"].ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
    }
    }
    }
    复制代码

    TextChanged事件:

    复制代码
    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
    //屏蔽中文输入和非法字符粘贴输入
    TextBox textBox = sender as TextBox;
    TextChange[] change = new TextChange[e.Changes.Count];
    e.Changes.CopyTo(change, 0);

    int offset = change[0].Offset;
    if (change[0].AddedLength > 0)
    {
    double num = 0;
    if (!Double.TryParse(textBox.Text, out num))
    {
    textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
    textBox.Select(offset, 0);
    }
    }
    }
    复制代码

    在网上有不少关入这方面的资料,下面是我选用的一个方案

    public NumberTextBox()
            {
                InitializeComponent();
                this.KeyDown += NumberTextBox_KeyDown;
                this.TextChanged += NumberTextBox_TextChanged;
            }

            void NumberTextBox_KeyDown(object sender, KeyEventArgs e)
            {
                if ((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }

            void NumberTextBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                TextChange[] change = new TextChange[e.Changes.Count];
                e.Changes.CopyTo(change, 0);

                int offset = change[0].Offset;
                if (change[0].AddedLength > 0)
                {
                    double num = 0;
                    if (!Double.TryParse(this.Text, out num))
                    {
                        this.Text = this.Text.Remove(offset, change[0].AddedLength);
                        this.Select(offset, 0);
                    }
                }
            }

    但是这个方案当输入法为中文的时候效果不理想,于是想到了禁用输入法。

    引入xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"

     然后再 TextBox控件的xaml中  input:InputMethod.IsInputMethodEnabled="False" 就可以了。

  • 相关阅读:
    php 下设置cookie问题
    js 页面无滚动条添加滚轮事件
    Python中关于字符串的问题
    Python 字符串相加问题
    ajax 同步和异步
    重叠div鼠标经过事件
    Myeclipse中将项目上传到码云
    eclipse debug的时候提示debug Edit Source Lookup path
    阿里云+wordpress搭建个人博客网站
    centos7 安装mysql
  • 原文地址:https://www.cnblogs.com/changbaishan/p/4241557.html
Copyright © 2020-2023  润新知