非淡泊无以明志,非宁静无以致远 -心静如止水,动于静

Only Numbers in TextBox(摘录)

事情虽然小,但还是值得保留

http://www.ganshani.com/2008/04/10/only-numbers-in-textbox/

 

Requirement: TextBox in C# should accept only numbers as input.

Solution:

Raise an event KeyPress and paste following line of code.

private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
{
              if (!Char.IsNumber(e.KeyChar))
                       e.Handled = true;
}

Here, e stands for the arguments of the function and e contains several attributes and methods like:

e.KeyChar : Character that has been pressed
e.Handled  : Whether it is an acceptable character or not.

posted @ 2012-10-18 11:16  烟雨客  阅读(157)  评论(0编辑  收藏  举报