471,321 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,321 software developers and data experts.

Text Input Problem

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

Nov 28 '05 #1
6 1691
On Mon, 28 Nov 2005 07:40:03 -0800, alexmaster_2004
<al************@discussions.microsoft.com> wrote:
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?
--
Ludwig
http://www.goedgenoegen.be
Nov 28 '05 #2
Louis, Thanks for reply.
but i want to use a textbox.

Nov 28 '05 #3

"alexmaster_2004" <al************@discussions.microsoft.com> wrote in
message news:50**********************************@microsof t.com...
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
Nov 28 '05 #4
Hi,

You use KeyDown, no KeyPress

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Simon Watson" <simon dot watson at sage dot com> wrote in message
news:ek*************@TK2MSFTNGP09.phx.gbl...

"alexmaster_2004" <al************@discussions.microsoft.com> wrote in
message news:50**********************************@microsof t.com...
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

Nov 28 '05 #5
".";
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.
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Nov 29 '05 #6
> "Simon Watson" <simon dot watson at sage dot com> wrote in message
news:ek*************@TK2MSFTNGP09.phx.gbl...

"alexmaster_2004" <al************@discussions.microsoft.com> wrote in
message news:50**********************************@microsof t.com...
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



"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:ut****************@TK2MSFTNGP15.phx.gbl... Hi,

You use KeyDown, no KeyPress

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


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
Nov 29 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by j.t.w | last post: by
5 posts views Thread by simon_s_li | last post: by
3 posts views Thread by dugald.morrow | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.