Q: Masking a TextBox

  • Thread starter Thread starter Martin
  • Start date Start date
M

Martin

Hi!

The CompactFramework doesnt seem to have the TextBox where you can use
masks.

What kan i do to allow only numbers in a TextBox? Or could i set the keypad
to
just accept numbers instead of alpha chars? If so, how, links, direction
would be great.

Regards
Martin
 
You have to subclass the TextBox class and override the virtual method
OnKeyPress and examine the KeyPressEventArgs for numeric's. Something like
the following would work for the OnKeyPress implementation:

C#:
protected override void OnKeyPress(KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case (char)Keys.D0:
case (char)Keys.D1:
case (char)Keys.D2:
case (char)Keys.D3:
case (char)Keys.D4:
case (char)Keys.D5:
case (char)Keys.D6:
case (char)Keys.D7:
case (char)Keys.D8:
case (char)Keys.D9:
base.OnKeyPress(e);
break;
default:
e.Handled = true;
break;
}
}
 
Or add a KeyPress handler to the textbox and cancel the key if it's not
numeric:


myTextBox.KeyPress += new KeyPressEventHandler(myTextBox_KeyPress);
.....

void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if((e.KeyChar < '0') || (e.KeyChar > '9')) e.Handled = true;
}



--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com
 
I wouldn't say it's any better - just different. All depends on the larger
goal of the app and developer preference. If you need a bunch of them and
you already have the custom control written, your route would probably be
easier. For a one-off, mine is less code (and I'm lazy).


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com
 
It's also possible using some DllImport

---
[DllImport("coredll.dll", EntryPoint="GetFocus", SetLastError = true)]
private static extern IntPtr GetFocus();

[DllImport("coredll.dll", EntryPoint="SetWindowLong", SetLastError =
true)]
private extern static int SetWindowLong(IntPtr Hwnd, int Index, int
NewIndex);

[DllImport("coredll.dll", EntryPoint="GetWindowLong", SetLastError =
true)]
private extern static int GetWindowLong(IntPtr Hwnd, int Index);

public static void SetTextBoxNumeric(TextBox textBox){
textBox.Focus();
IntPtr hwnd = GetFocus();
int defaultStyle = GetWindowLong(hwnd, GWL_STYLE);
SetWindowLong(hwnd, GWL_STYLE, defaultStyle|ES_NUMBER);
}
 
Yes, but *only* for a very specific behavior. Foir example, if you want to
allow decimals, negative values or hex chanracters, setting the ES_NUMBER
style bit won't work.
 
This would fail with cut-n-paste.


Or add a KeyPress handler to the textbox and cancel the key if it's not
numeric:


myTextBox.KeyPress += new KeyPressEventHandler(myTextBox_KeyPress);
....

void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if((e.KeyChar < '0') || (e.KeyChar > '9')) e.Handled = true;
}



--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com
 
I found an easiest way to set a textbox at code as numeric, on load of
the form for example i set the following for a control named
NumTextBox:

InputModeEditor.SetInputMode(MovilTextBox, InputMode.Numeric);

Gustavo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Masked TextBox 3
Input mask for TextBoxes 1
mask to textbox 2
Q: How To Set a mask on a TextBox 2
Input Mask 1
mask textbox problems 1
Unable To Read Value in Masked Textbox 1
TextBox with custom OnPaint 0

Back
Top