digit input only in text box

  • Thread starter Thread starter Mike Kitchen
  • Start date Start date
M

Mike Kitchen

Hi David

Have alook at the textbox KeyPress event, then inspect KeyEventArgs
parameter and interogate which key has been pressed.

The following code may help

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
const char Delete = (char)8;
e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
}

Use "e.Handled = true;" to stop characters being accepted.

Hope this helps

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html
 
You can create a digital textBox class. The trouble is, that under certain
circumstances,
the MS IDE might decide to throw out all your digital textboxes, created in
that way.
That may cause one hell of a job to put things right again. At least, that
was my exerience.

So what I do in stead is this

#region ...
<come code>
this.textBox1 += new
System.Windows.Forms.KeyPressEventHandler(this.numbersCheck);
<some code>
#endregion

<somewhere else>
private void numbersChecked(Object o, KeyPressEventArgs e)
{
if(!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar()
e.Handled is true;
}

************************************************
 
hello, little newbie question,
i'm tring to create textbox control with ability do write there only
digits'.
i tried to check at -left focus- event, but it's not that i really want, i
want not to be able even to write to control anything but digits'...

how can i do that? or there can i find any sample
 
Hi,

You can handle the keypress event of the textbox. Based on the keycode
pressed, you can either block or continue with the key.

An example (in VB.NET) could be found here:
http://www.knowdotnet.com/articles/numerictextboxes.html


hello, little newbie question,
i'm tring to create textbox control with ability do write there only
digits'.
i tried to check at -left focus- event, but it's not that i really want, i
want not to be able even to write to control anything but digits'...

how can i do that? or there can i find any sample
 
I created my own numeric text box, and it is working properly. Basically it
overrides OnKeyPress and OnKeyDown protected methods and formats text.
The only problem i had with it is, when i put it on the windows form in VS
IDE, text becomes "numericTextBox1" although i did some initializations in
the constructor saying
public NumericTextBox()
{
Text = FormatText(0);
...
}
How can i hanlde this?
 
Greetings.

i'm tring to create textbox control with ability do write there only
digits'.
i tried to check at -left focus- event, but it's not that i really want, i
want not to be able even to write to control anything but digits'...

Check the KeyDown event. It should be what you are looking for.

BR
Mariusz Wojsyk
 
Back
Top