Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends

With the help of our friends, we designed a component:
C#
int t=0;
protected override void OnKeyPress(KeyPressEventArgs e)
{
    string s = e.KeyChar.ToString();
    if (!(e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '.' && t < 1 || char.IsControl(e.KeyChar)))
    {
        e.Handled = true;
    }
    if (e.KeyChar == '.') t++;
    base.OnKeyPress(e);
}

this text box only get decimal number.

a problem :if write in textBox and then delete it >> textBox don't get "." because counter t >0
how can set t=0 where textBox cleared??
Posted
Updated 14-May-12 0:05am
v5

Better react on the TextChanged event, and check the present text.
By the way, a decimal point is ok for US settings, but not for all countries (in Germany we use a decimal comma) - use CurrentUICulture.NumberFormat.NumberDecimalSeparator or CurrencyDecimalSeparator.
And of course text could be added to your textbox by copy/paste. Or by a WM_SETTEXT message, or whatever is implemented in some assistive technology...
 
Share this answer
 
Comments
VJ Reddy 14-May-12 12:00pm    
Good points. 5!
The Solution 1 and 2 are good.

I want to add that the KeyDown event can be used to prevent entry of other than numbers and decimal but at the same time allow use of keys like Del, BackSpace etc.

If it is required to enable pasting only decimal number, then I think the WndProc method explained here WndProc method[^] is to be handled.

The list of Windows Messages are given here http://wiki.winehq.org/List_Of_Windows_Messages[^]

The text to be pasted is combined with the existing text and tested with Regex with pattern "^\d*\.?\d*$" before being pasted, to see that the entire text contains only digits and one or none decimal.

Since, WndProc is a protected method, the TextBox is to be sub classed to provide the desired functionality as shown below:

C#
public class DecimalTextBox : TextBox {
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        try {
            if (m.Msg !=0x302) {
                base.WndProc(ref m);
                return;
            }
            string modifiedText = Text + Clipboard.GetText();
            if (Regex.IsMatch(modifiedText,@"^\d*\.?\d*$",
                    RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)){
                Text = modifiedText;
                SelectionStart = Text.Length;
            }
        } catch  {

        }
    }

    protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs args) {
        if (args.Control || args.Alt)
            return;

        switch(args.KeyCode){
            case Keys.End:
            case Keys.Home:
            case Keys.Left:
            case Keys.Right:
            case Keys.Back:
            case Keys.Delete:
                return;
        }

        if (!char.IsDigit((char)args.KeyData) &&
            (args.KeyCode!= Keys.OemPeriod ||
                (args.KeyCode == Keys.OemPeriod && Text.Contains(".")))){
            args.SuppressKeyPress=true;
        }
    }
}
 
Share this answer
 
Comments
faezun 14-May-12 14:54pm    
thanks
:)

do you test it
this textBox don't input anything(number or character)
VJ Reddy 14-May-12 16:38pm    
I tested it. And it's working fine. Allows only digits and only one decimal point.
Please check again.

Thank you
The first thing to do is to bracket your if condition to show exactly what you meant to happen:
C#
if (c1 && c2 || c3 && c4 || c5)

Did you want
C#
if ((c1 && c2) || (c3 && c4) || c5)

or
C#
if (c1 && (c2 || c3) && (c4 || c5))
It won't cure your problem, but it will make your code clearer and easier to read.

I probably wouldn't do it that way: there are way too many things you need to check for. Delete key, backspace key, The keyboard Cut and paste keys (all four of them), and the mouse cut and paste as well.

Instead, I would probably handle the TextChanged event instead, and sort it out there.
 
Share this answer
 
Comments
VJ Reddy 14-May-12 12:00pm    
Nice explanation. 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900