windows phone中textbox只能输入数字 .

本文介绍两种方法来限制WPF应用中的Textbox仅能输入数字。第一种是简单地设置Textbox的InputScope属性;第二种则是创建一个继承自Textbox的新类,并通过重写OnKeyDown方法实现对输入字符的过滤。

两种方法  

第一种比较简单,直接设置textbox的属性,属性是InputScope

第二种就比较麻烦,需要添加一个类,使该类继承于textbox,然后设置其只能输入数字,需要在xaml页面添加命名空间,然后引用就行了

设置类的代码:

View Code
 1 private readonly Key[] numeric = new Key[] { Key.Back, Key.NumPad0, Key.NumPad1, Key.NumPad2, Key.NumPad3, Key.NumPad4, Key.NumPad5, Key.NumPad6, Key.NumPad7, Key.NumPad8, Key.NumPad9 }; 
 2           public NumericTextBox()      
 3           {            //将文本设置为  电话号码的文本输入模式   
 4               this.Width = 300.0;
 5               this.Height = 80.0;
 6               this.InputScope = new InputScope();        
 7               this.InputScope.Names.Add(new InputScopeName() { NameValue = InputScopeNameValue.TelephoneNumber });  
 8           }   
 9          protected override void OnKeyDown(KeyEventArgs e)   
10          {  //如果是数字键或者返回键则设置e.Handled = true; 表示事件已经处理    
11              if(Array.IndexOf(numeric,e.Key) == -1)           
12              {               
13                  e.Handled = true;         
14              }           
15              base.OnKeyDown(e); // important, if not called the back button is not handled     
16          }

 

转载于:https://www.cnblogs.com/netqin/archive/2012/11/24/2786194.html

using RegexFormat; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using 学生管理系统数据库版.Model; namespace 学生管理系统数据库版.View.User { public partial class LoginF : Form { private bool phoneOK = false; private bool pswOK = false; public LoginF() { InitializeComponent(); comboBox1.SelectedIndex = 0; comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; button1.Enabled = false; } private void textBox1_TextChanged(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox1.Text)) { label4.Text = "请输入手机号"; label4.ForeColor = Color.Black; return; } bool isOK = FormatCheckTool.IsPhoneNumber(textBox1.Text); phoneOK = isOK; if (!isOK) { label4.Text = "手机号格式不正确"; label4.ForeColor = Color.Red; } else { label4.Text = "手机号格式正确"; label4.ForeColor = Color.Green; } button1.Enabled = phoneOK && pswOK; } private void button2_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; } private void textBox2_TextChanged(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox2.Text)) { label5.Text = "密码长度为6-10位,必须由字母、数字、符号组成"; label5.ForeColor = Color.Black; return; } bool isOK = FormatCheckTool.IsGoodPsw(textBox2.Text); pswOK = isOK; if (!isOK) { label5.Text = "密码格式不正确"; label5.ForeColor = Color.Red; } else { label5.Text = "密码格式正确"; label5.ForeColor = Color.Green; } button1.Enabled = phoneOK && pswOK; } private void button1_Click(object sender, EventArgs e) { int role = comboBox1.SelectedIndex; UserM user = new UserM(); user.Phone = textBox1.Text; user.Psw = textBox2.Text; user.Role = role; } } } 我需要用MySqL连接
06-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值