Thursday, August 28, 2008

Numeric TextBox and One Period

Hello guys.

I have sample here to show how to implement a numeric only to your textbox. tnx to mr.solitaire for the idea.

Put this code to your textbox in keypress.


bool dot = false; /for period
bool flag = false; /for minus

if (e.KeyChar == '\b') return;
if (e.KeyChar == '.' & textBox1.Text.IndexOf('.') > 0) dot = true;
if (textBox1.Text.StartsWith(".")) /if its dot first then
{ textBox1.Text = "0."; textBox1.SelectionStart = 3; }
if (e.KeyChar < '-' e.KeyChar > '9' dot == true) flag = true;
if (textBox1.Text.Length > 0 & e.KeyChar == '-') flag = true;
if (e.KeyChar == '/') flag = true;
if (flag == true) e.Handled = true;


Hope this one will help.

No comments: