mohasad

hi all ,
i have included a texbox in my form . when inputting the numbers to textbox , i cannot able to input the correct numbers . moreover i want the text that to be given as input should be restricted to only alpha numeric . is there any such possibilities .
thanks
sadiq



Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

Stephen Hauck

off the top of my head

if you look at the following section in the help file "ASCII Character Codes Chart 1"

as you can see the ASCII values for 0-9 are 48-57 inclusive

for a-z they are 97-122 inclusive

for A-Z they are 65-90 inclusive

define the ranges any way you want to and check the ASCII value in the KeyDown event for inclusion / exclusion





Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

JimMcGowanInlet

You didn't say VB or C. But here is what I do for VB.

Set KeyPreview=True in Form.Load

Code Snippet

Private Sub BillTotal_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles BillTotal.KeyPress

Select Case e.KeyChar

Case ChrW(8), ChrW(46)

'Backspace, Decimal point

Case ChrW(13)

Call Calculate()

Case Else

If Not Char.IsNumber(e.KeyChar) Then e.Handled = True

End Select

End Sub





Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

mohasad

hi ,
could you post the above code in c#. because i don know VB.
thanks
sadiq





Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

JimMcGowanInlet

Sorry I have the oposite problem. I don't know C.





Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

Ilya Tumanov

Here's good reading for you both then. The reality is what VB.Net and C# are probably just close as US English and UK English.

There¡¯s also a number of online translators available which you can use.






Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

Christopher Fairbairn

Here's the equivalent in C#

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

{

switch (e.KeyChar)

{

case '\u0008': // Backspace

case '.': // Decimal point

Calculate();

break;

case '\r': // Carrage Return

Calculate();

break;

default:

if (!Char.IsNumber(e.KeyChar))

e.Handled = true;

break;

}

}

Thanks,

Christopher Fairbairn






Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

mohasad

hi christopher,

my problem is that when the user inputs mobile no , only the numbers should display . i.e
on pressing '5' only '5' should be displayed , but in my case , if i want to input '5' to textbox , i have to press the key for 4 times, since the key '5' has the characters such as 'j','k','l' . i think i have clearly explained the problem . is there any possiblity the above issues can be accomplished .
thanks
sadiq





Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

Christopher Fairbairn

Hi sadiq,

Sorry I mis-interpreted your question before. I think you will have to get your "hands dirty" and send a message to the native edit control which the TextBox class within System.Windows.Forms wraps in order to access functionality present in the underlying operating system to achieve your goal.

Include a reference to the Microsoft.WindowsCE.Forms.dll assembly in your project and then make use the following code:

using Microsoft.WindowsCE.Forms;

private static readonly int EM_SETINPUTMODE = 0x00DE;

private static readonly IntPtr EIM_NUMBERS = (IntPtr)2;

protected void SetInputMode(TextBox tb)

{

Message m = new Message();

m.HWnd = tb.Handle;

m.Msg = EM_SETINPUTMODE;

m.WParam = IntPtr.Zero;

m.LParam = EIM_NUMBERS;

MessageWindow.SendMessage(ref m);

}

So if you have a TextBox called textBox1 you simply would make a call such as:

SetInputMode(textBox1);

within your form's constructor (after the call to InitializeComponent is a good spot). This should set the input mode for the text box to numeric only and have the desired effect.

If you are interested in what this code is doing then have a look at the following MSDN documentation page http://msdn2.microsoft.com/en-us/library/ms834662.aspx it outlines the EM_SETINPUTMODE message in detail as well as a few other values you can use instead of EIM_NUMBERS.

Thanks,

Christopher Fairbairn






Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

Zero Dai - MSFT

Hi sadiq,

I catch your idea! You are developing an application for Smart Phone! That's good enough!

I think InputModeEditor.SetInputMode is good enough for you. It is used to specifies the input mode on a Smart Phone (only supported on Smart Phone & Provided in .net Compact Framework 2.0).

Zero Dai - MSFT






Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

Christopher Fairbairn

Hi Zero Dai,

I don't know how I hadn't spotted that class before :-P Handy to know about.

Thanks,

Christopher Fairbairn






Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

mohasad

hi christopher,
one more doubt in your code that why u have used the following assignment :
private static readonly int EM_SETINPUTMODE = 0x00DE;
private static readonly IntPtr EIM_NUMBERS = (IntPtr)2;

so if i use EIM_SPELL or EIM_AMBIG what should be assigned . More over







Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

Christopher Fairbairn

Hi sadiq,

The Compact Framework's System.Windows.Forms implementation is typically a wrapper around the underlying Win32 APIs.

In my post I mentioned the documentation on MSDN located at http://msdn2.microsoft.com/en-us/library/ms834662.aspx. This is documenting the window message which can be sent to a native edit control (text box) to set the input mode. It will in essence be what InputModeEditor.SetInputMode is doing under the covers so to speak.

Towards the bottom of that page you will find a section listing the requirements for this message, and in particular which header file you must include if you are developing in C or C++.

Header: winuserm.h

This means all the definitions such as EM_SETINPUTMODE and EIM_NUMBERS are contained within the file winuserm.h which is installed as part of your Windows Mobile SDK installation. Assuming default installation folders etc you can find this file on your machine at C:\Program Files\Microsoft Visual Studio 8\SmartDevices\SDK\PocketPC2003\Include\winuserm.h

Within this header file you will see definitions such as the following

#define EM_SETINPUTMODE 0x00DE // Sets default input mode when control gets focus. lParam should be be EIM_*|EIMMF_*.

#define IM_SPELL 0

#define IM_AMBIG 1

#define IM_NUMBERS 2

#define IM_LAST IM_NUMBERS

#define IM_MASK 0x0000FFFF // Mask to access mode.

// Input modes for EM_SETINPUTMODE/EM_GETINPUTMODE

#define EIM_SPELL IM_SPELL // Spell input mode

#define EIM_AMBIG IM_AMBIG // Ambiguous mode if input engine supports this mode. Else defaults to Spell mode.

#define EIM_NUMBERS IM_NUMBERS // Numbers mode.

#define EIM_TEXT (IM_LAST + 1) // User's preferred mode. This is the user's last Spell or Ambig mode selection

// in an EIM_TEXT edit control.

The readonly constants I placed into my C# example were purely the translation of these C preprocessor definitions into C# syntax. As you see in my C# code I gave EM_SETINPUTMODE the value 0x00DE which is exactly the value you will find in this C header file.

So to answer your question, EIM_SPELL would be 0 and EIM_AMBIG would be 1, i.e.

private static readonly IntPtr EIM_SPELL = (IntPtr)0

private static readonly IntPtr EIM_AMBIG = (IntPtr)1

When viewing articles on the internet about the Compact Framework you are bound to come across examples of native interop which can take many different forms such as sending window messages or Platform Invoking native methods etc. This is the approach that most people will take to get the values required for such calls. The values seem rather cryptic unless you know how they are derived/obtained, we're simply duplicating the contents of the C header files provided by Microsoft into a form usable by C#.

So i guess with these postings you can see how you could do it with Compact Framework version 1.0 if you had to (with slight code changes due to the missing Handle property on the TextBox control in CF1.0), and how later versions such as Compact Framework v2.0 have continued to wrap up more of the functionality to remove the need for this kind of thing for most cases.

Hope this helps,

Christopher Fairbairn






Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

mohasad

hi Ilya,
hi all ,
i have been from smartphone development . i want to input numbers , so planned to use buttons for 0-9 . i want to display 1 when i press button name "1" and so on .. that is i want all the buttons to act as way to input numbers . is there any other way to do so . if so give me some link to learn .

thanks
sadiq





Re: Smart Devices VB and C# Projects restricting text in TextBox Control to Alpha Numeric

mohasad

hi all ,
i have been from smartphone development . i want to input numbers , so planned to use buttons for 0-9 . i want to display 1 when i press button name "1" and so on .. that is i want all the buttons to act as way to input numbers . is there any other way to do so . if so give me some link to learn .

thanks
sadiq