473,509 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replacing keyboard input in textbox

Hi,

How can I restrict the keyboard input in a textBox to 0..9? In the
keydown event KeyValue is get only. Where can I alter the keyboard input?

Mvg,
Nov 17 '05 #1
8 5028
Jo,

Use the keyup event and than by instance the e.value
(Don't forget on by instance value 8 because that is the backspace).

I hope this helps,

Cor
Nov 17 '05 #2
Cor Ligthert schreef:
Jo,

Use the keyup event and than by instance the e.value
(Don't forget on by instance value 8 because that is the backspace).

I hope this helps,

Cor

Hi Cor,

This is the code I use:

private void textBoxOrderid_KeyUp(object sender,
system.Windows.Forms.KeyEventArgs e)
{
if ((char)e.KeyValue < '0' || (char)e.KeyValue > '9' || e.KeyValue == 8)
{
e.Handled = true;
}
}

I only want the keys 0 to 9 to appear in the textbox. However the method
above does not work.

Any other suggestions?
Jo.
Nov 17 '05 #3
Override TextBox class like following :

class TextBoxNum:TextBox {
private String _Text="";
private Regex regex=new Regex("^[0-9]*$",RegexOptions.Compiled);
protected override void OnTextChanged(EventArgs e) {
if (Text==_Text) return ;//Don't recurs
if (regex.Match(Text).Success) _Text=Text;
else {
int pos=SelectionStart+_Text.Length-Text.Length;
Text=_Text;
SelectionStart=pos<0?0:pos;
}
}
}
}

And declare in your code textBoxOrderid as a TextBoxNum :
TextBoxNum textBoxOrderid
Hope it helps,

Ludovic SOEUR.
"Jo Segers" <jo*******@alro.be> a écrit dans le message de
news:Ot**************@tk2msftngp13.phx.gbl...
Hi,

How can I restrict the keyboard input in a textBox to 0..9? In the
keydown event KeyValue is get only. Where can I alter the keyboard input?

Mvg,

Nov 17 '05 #4
Jo,

Is it not something more as this that you want.
{
if ((char)e.KeyValue < '0' || (char)e.KeyValue > '9' || e.KeyValue == 8)
{
textBox1.SelectionStart = (int)textBox1.Text.Length - 1;
textBox1.SelectionLength = 1;
e.Handled = true;
}
}


Be aware that you should use the validating event as well to check that
nobody paste something wrong in, or set the property that allows that to
false.

I hope this helps,

Cor
Nov 17 '05 #5
Cor Ligthert schreef:
Jo,

Is it not something more as this that you want.

{
if ((char)e.KeyValue < '0' || (char)e.KeyValue > '9' || e.KeyValue == 8)
{

textBox1.SelectionStart = (int)textBox1.Text.Length - 1;
textBox1.SelectionLength = 1;

e.Handled = true;
}
}

Be aware that you should use the validating event as well to check that
nobody paste something wrong in, or set the property that allows that to
false.

I hope this helps,

Cor

Thanks a lot for the usefull information.

Yours sincerely,

Jo Segers.
Nov 17 '05 #6
Override TextBox class like following :

class TextBoxNum:TextBox {
private String _Text="";
private Regex regex=new Regex("^[0-9]*$",RegexOptions.Compiled);
protected override void OnTextChanged(EventArgs e) {
if (Text==_Text) return ;//Don't recurs
if (regex.Match(Text).Success) _Text=Text;
else {
int pos=SelectionStart+_Text.Length-Text.Length;
Text=_Text;
SelectionStart=pos<0?0:pos;
}
}
}
}

And declare in your code textBoxOrderid as a TextBoxNum :
TextBoxNum textBoxOrderid
Hope this helps,

Ludovic SOEUR.

"Jo Segers" <jo*******@alro.be> a écrit dans le message de
news:ei**************@TK2MSFTNGP09.phx.gbl...
Cor Ligthert schreef:
Jo,

Is it not something more as this that you want.

{
if ((char)e.KeyValue < '0' || (char)e.KeyValue > '9' || e.KeyValue == 8)
{

textBox1.SelectionStart = (int)textBox1.Text.Length - 1;
textBox1.SelectionLength = 1;

e.Handled = true;
}
}

Be aware that you should use the validating event as well to check that
nobody paste something wrong in, or set the property that allows that to
false.

I hope this helps,

Cor

Thanks a lot for the usefull information.

Yours sincerely,

Jo Segers.

Nov 17 '05 #7
Ludovic SOEUR schreef:
Override TextBox class like following :

class TextBoxNum:TextBox {
private String _Text="";
private Regex regex=new Regex("^[0-9]*$",RegexOptions.Compiled);
protected override void OnTextChanged(EventArgs e) {
if (Text==_Text) return ;//Don't recurs
if (regex.Match(Text).Success) _Text=Text;
else {
int pos=SelectionStart+_Text.Length-Text.Length;
Text=_Text;
SelectionStart=pos<0?0:pos;
}
}
}
}

And declare in your code textBoxOrderid as a TextBoxNum :
TextBoxNum textBoxOrderid
Hope this helps,

Ludovic SOEUR.


Thanks for all the information. I got it working the following way:

private void textBoxOrderid_TextChanged(object sender, System.EventArgs e)
{
string temp = textBoxOrderid.Text;
string newval = "";
for (int i = 0; i < temp.Length; i++)
{
if (temp[i] >= '0' && temp[i] <= '9')
{
newval += temp[i];
}
}
if (temp != newval)
{
textBoxOrderid.Text = newval;
textBoxOrderid.SelectionStart = textBoxOrderid.Text.Length;
}
}

Any remarks?

Jo.
Nov 17 '05 #8
OK, that's nearly the same but you should keep Regex instead of your loop if
you need efficiency.

"Jo Segers" <jo*******@alro.be> a écrit dans le message de
news:e9**************@TK2MSFTNGP14.phx.gbl...
Ludovic SOEUR schreef:
Override TextBox class like following :

class TextBoxNum:TextBox {
private String _Text="";
private Regex regex=new Regex("^[0-9]*$",RegexOptions.Compiled);
protected override void OnTextChanged(EventArgs e) {
if (Text==_Text) return ;//Don't recurs
if (regex.Match(Text).Success) _Text=Text;
else {
int pos=SelectionStart+_Text.Length-Text.Length;
Text=_Text;
SelectionStart=pos<0?0:pos;
}
}
}
}

And declare in your code textBoxOrderid as a TextBoxNum :
TextBoxNum textBoxOrderid
Hope this helps,

Ludovic SOEUR.


Thanks for all the information. I got it working the following way:

private void textBoxOrderid_TextChanged(object sender, System.EventArgs e)
{
string temp = textBoxOrderid.Text;
string newval = "";
for (int i = 0; i < temp.Length; i++)
{
if (temp[i] >= '0' && temp[i] <= '9')
{
newval += temp[i];
}
}
if (temp != newval)
{
textBoxOrderid.Text = newval;
textBoxOrderid.SelectionStart = textBoxOrderid.Text.Length;
}
}

Any remarks?

Jo.

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
32924
by: Ralf Toender | last post by:
Hi! Does anybody know how to send a keystroke C#? In C++ it's done by: keybd_event ( ... ) or SendInput ( ... ). What namespace does include this counterpart? Thanks Ralf
7
10599
by: Don Riesbeck Jr. | last post by:
I'm working on an application (OEM) using C# that utilizes input from a keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device, and input from it is sent to the system as if it...
2
10194
by: DDK | last post by:
Question (using C# and ASP.NET), I have a textbox and a buttonClick on my asp.net page. I would like be able to hit the Enter button on the keyboard and have it do the same thing as the...
2
4423
by: Uveper | last post by:
I am trying to create a textbox which would have a delayed input from keyboard. For example when I press "A" it waits for 200 ms and only after that time it passes the key to textbox. I tryed to do...
4
4056
by: davermcl | last post by:
Hi, I'm experiencing a problem when using the VB SendKeys method. I'm sending characters to a textbox in another application. It works fine when the Windows Input Languages on both apps are...
0
2720
by: neonspark | last post by:
I'm buidling some simple macro functionality for my app so the users can record a sequence of keyboard inputs and replay them reliably via some menu. Originally, I used: protected override bool...
9
10957
by: Kbalz | last post by:
I have an application that minimizes itself, and I want it to listen for certain key commands, and when they are pressed, my program can react to them. So far I've gotten my application to react...
1
3072
by: H-S | last post by:
Help! I am trying to use the Windows.Controls.TextBox with spellcheck turned on. It works fine if my keyboard input language is set to English (United States). But the spellcheck starts to get...
8
5284
by: BD | last post by:
How can I duplicate the behavior of the operating system shortcut keys in my application? For example, my windows form has 5 controls (textboxes), the operating system will pickup which control...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.