silverlight—TextBox只能輸入數字或小數 屏蔽中文輸入和非法粘貼: KeyDown事件處理方法

來源:互聯網
上載者:User

 private void txtRoomNum_KeyDown(object sender, KeyEventArgs e)
{
    TextBox txt = sender as TextBox;
    //屏蔽非法按鍵,只能輸入整數
    if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
    {
        e.Handled = false;
    }
    else
    {
        e.Handled = true;
    }
}

private void txtRoomArea_KeyDown(object sender, KeyEventArgs e)
{
    TextBox txt = sender as TextBox;
    //屏蔽非法按鍵,只能輸入小數
    if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal)
    {
        if (txt.Text.Contains(".") && e.Key == Key.Decimal)
        {
            e.Handled = true;
            return;
        }
        e.Handled = false;
    }
    else
    {
        e.Handled = true;
    }

}

 

 

參考一:
以下實現TextBox只能輸入小數並且屏蔽中文輸入和非法粘貼:
說明:以下實現均在Framework 3.0平台下

為TextBox加兩個事件:TextChanged和KeyDown事件,具體如下:
KeyDown事件:

 1private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
 2        {
 3            TextBox txt = sender as TextBox;
 4
 5             //屏蔽非法按鍵
 6            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal)
 7            {
 8                if (txt.Text.Contains(".") && e.Key == Key.Decimal)
 9                {
10                    e.Handled = true;
11                    return;
12                }
13                e.Handled = false;
14            }
15            else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
16            {
17                if (txt.Text.Contains(".") && e.Key == Key.OemPeriod)
18                {
19                    e.Handled = true;
20                    return;
21                }
22                e.Handled = false;
23            }
24            else
25            {
26                e.Handled = true;
27            }
28        }
29
30

TextChanged事件 1private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
 2        {
 3            //屏蔽中文輸入和粘貼輸入
 4            TextBox textBox = sender as TextBox;
 5            TextChange[] change = new TextChange[e.Changes.Count];
 6            e.Changes.CopyTo(change, 0);
 7
 8            int offset = change[0].Offset;
 9            if (change[0].AddedLength > 0)
10            {
11                double num = 0;
12                if (!Double.TryParse(textBox.Text, out num))
13                {
14                    textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
15                    textBox.Select(offset, 0);
16                }
17            }
18        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.