private void KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8))
e.Handled = true;
base.OnKeyPress(e);
}
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8))
e.Handled = true;
base.OnKeyPress(e);
}
解决方案 »
- winform 是否可以模拟真实调用事件?
- 寻背包问题求解
- 求大牛 分析下批量框架的 想法和设计 什么建议或觉得不妥的地方写下来
- C# 窗体吸附
- 一个Repeater 与 DataTable 绑定的问题
- 请教:软件在没有安装VS2005的电脑上为什么不能运行?
- 并非所有的代码路径都返回值
- label控件怎么继承一个shape类?
- 求c# wince下 webBrowser控件中网页显示及保存问题
- 各位弟弟妹妹们,大姐在退休前想学会C#,也许是我今生所学的最后一门语言了,请各位祝我今年心想事成。
- 我的VS.NET2002中第二张碟有个文件读不出来,如果我下一张该碟的ISO并装入虚拟光驱中,能顺利安装成功吗?
- C#中的布尔类型怎么用?(我是C#菜鸟)
using System.ComponentModel;
using System.ComponentModel .Design ;namespace contr
{
/// <summary>
///
/// </summary>
public class TextBoxNum : System.Windows.Forms.TextBox
{
public delegate void ValueChangedHandler(object o,decimal DecValue);
public event ValueChangedHandler ValueChanged;
public int MaxInt=8;
public int MaxLittle=2;
public decimal DecimalValue=0;
public TextBoxNum()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private void InitializeComponent()
{
}
protected override void OnTextChanged(EventArgs e)
{
if(this.Text .Length !=0&&(this.Text[0])=='.')
{
this.Text ="0.";
this.SelectionStart=(this.Text .Length );
return;
}
string s=this.Text.Trim ();
if(this.MaxLittle ==0)
{
if(s.IndexOf ('.')>0||this.Text .Length >this.MaxInt )
{
this.Text =this.ValueDecimal .ToString ();
this.SelectionStart=(this.Text .Length );
return; }
}
if(s ==""){return;}
int j=s .IndexOf ('.');
if((j<0&&s.Length >this.MaxInt ))
{
this.Text =this.ValueDecimal .ToString ();
this.SelectionStart=(this.Text .Length );
return;
}
if(j>-1&&(s.Length -j-1)>this.MaxLittle)
{
this.Text =this.ValueDecimal .ToString ();
this.SelectionStart=(this.Text .Length );
return;
}
int a=0;
char[] temp=s.ToCharArray () ;
for(int i=0;i<s.Length ;i++)
{
if(Char.IsDigit(temp[i]))
{
continue;
}
else if(temp[i]=='.')
{
a++;
if(a>1)
{
this.Text =this.ValueDecimal .ToString ();
this.SelectionStart=(this.Text .Length );
return;
}
continue;
}
this.Text =this.ValueDecimal .ToString ();
this.SelectionStart=(this.Text .Length );
return;
}
this.ValueDecimal =decimal.Parse (this.Text);
//TextChanged (e);
} public event ValueChangedHandler DecmalValueChanged
{
add
{
this.ValueChanged += value;
}
remove
{
this.ValueChanged -= value;
}
}
public decimal ValueDecimal
{
get
{
return this.DecimalValue ;
}
set
{
this.DecimalValue =value;
if(ValueChanged!=null)
{
ValueChanged(this,this.DecimalValue );
}
}
}
[Browsable(true),DefaultValue(8)]
public int MaxOfInt
{
get
{
return this.MaxInt ;
}
set
{
if(value<=29)
{
this.MaxInt =value;
this.MaxLength =this.MaxLittle +value +1;
}
else
{
this.MaxInt =29;
this.MaxLength =this.MaxLittle +29 +1;
}
}
}
[Browsable(true),DefaultValue(2)]
public int MaxOfLittle
{
get
{
return this.MaxLittle ;
}
set
{
if(value<=29)
{
this.MaxLittle =value;
this.MaxLength =value +this.MaxInt +1;
}
else
{
this.MaxLittle =29;
this.MaxLength =29 +this.MaxInt +1;
}
}
}
}
}
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
{If Regex.IsMatch(TextBox3.Text, "^\d*$") Then
MsgBox("OK!!");
Else
TextBox3.Text = "";
}
if("0123456789".IndexOf(e.keyChar) == 1 && e.keyChar != (char)8)
{
MessageBox.show("no number !");
e.Handel = true ;
}its can do it .But i think 正则表达式 best !
c#不认
michaelowenii(少年狂) 的方法不能防止非数字的粘贴
using System.Text.RegularExpressions;
private void button1_Click(object sender, System.EventArgs e)
{
Regex digitregex = new Regex("(^[0-9]$)");
if (digitregex.IsMatch(textBox1.Text))
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("PLZ Input number[0-9]");
}
}
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not (IsNumeric(e.KeyChar) Or e.KeyChar = Microsoft.VisualBasic.ChrW(8) Or e.KeyChar = Microsoft.VisualBasic.ChrW(46)) Then
e.Handled = True
End If
End Sub
结合正则表达式!!!-------努力学习 不断实践 虚心讨教--------
{
if(ch.IsDigit())
.....
else
.....
}
1可以用ascii判断,
2也可以用char.IsDigit(),IsNum()等等来判断,
3也可以通过try{}catch{}把字符转换为数字来判断,如果产生异常那么说明你输入的不是数字了
方法多的是,还有其他的方法,至于其他的,你可以自己探究