Text Input Problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
i want to know how i can make the TextBox control just accept numbers and ".";
EX:
the user can enter "1" OR "1.4"
Hope anyone could help
Thanks
 
Hi
i want to know how i can make the TextBox control just accept numbers and ".";
EX:
the user can enter "1" OR "1.4"
Hope anyone could help
Thanks

Why don't you use numericupdowncontrol?
 
alexmaster_2004 said:
Louis, Thanks for reply.
but i want to use a textbox.

First, please don't delete old text from the post, it means you have to look
through previous posts in the thread, rather than opening the last one :-)

Here's my suggestion:

You can do this via a two-pronged approach, using the keypress and changed
events in conjunction. First you need to look at how the value in a textbox
can be changed:

Keypress.
Paste via keyboard
Paste via context menu
Drag-drop (if implemented)
Changed in code.

In the keypress event, if the user presses a printable character and it's
not one you like, set the handled flag on the eventargs to true, as this
will stop the keypress getting into the textbox.

In the changed event handler, you need to know that the change hasn't first
been through your keypress handler with a valid character. If it has, do
nothing. If it hasn't, check if the new changed value is valid or not. If it
isn't, reset the value in the textbox to it previous (you need to store
this) valid value.

cheers

Simon
 
".";
EX:
the user can enter "1" OR "1.4"
Hope anyone could help
Make sure you accept the decimal separator matching the current user locales,
not dot.
Comma is used as decimal separator in most of Europe and in many other
countries.
 
Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

You use KeyDown, no KeyPress



cheers,

If you set handled to true on the keydown event eventargs, the character
still makes it to the textbox. If you set handled to true on the keypress
event eventargs, they key pressed is not passed on to the textbox.

As a simple example:

private void textBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
e.Handled = true;
}

all keypresses are still propogated to the text box.

private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = true;
}

no keypresses make it to the textbox. Also, I don't think the keydown event
will deal with keyrepeat, will it?

this was useing vs2005 release version

cheers

Simon
 
Back
Top