Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,
i need a textbox in my windows application which accept only numbers and only one character 'p', and the character 'p' should occur only one time.
How can i do this?
when paste into the textbox, it should not allow to enter other characters.

Thanks
Jiju
Posted

The Solution 1 to 4 given already are good and provide the functionality required to some extent.

But to validate the text being pasted I think the WndProc method explained here
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc.aspx[^]
is to be handled and to allow the copy, paste etc from key board KeyDown event is to be handled.

The list of Windows Messages are given here
List Of Windows Messages[^]

The text to be pasted is combined with the existing text and tested with Regex with pattern "^\d*p?\d*$" before being pasted.

Since, WndProc is a protected method, the TextBox is to sub classed to provide the desired functionality as shown below:
C#
public class NumTextBox : 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*p?\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.P || (args.KeyCode == Keys.P
                &&  Text.ToLower().Contains("p")))){
            args.SuppressKeyPress=true;
        }
    }
}
 
Share this answer
 
v3
Try this Code :-

if(e.KeyChar!=8 && e.KeyChar!=70)
{
   if(!char.IsDigit(e.KeyChar))
      e.Handled=true;
}


//8 for BackSpace and 70 for 'p'

Hope this work :)
 
Share this answer
 
Comments
VJ Reddy 13-May-12 7:44am    
Good answer. 5!
C#
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) || e.KeyChar == 'p')
            {

            }
            else
            {
                e.Handled = true;
            }
        }
 
Share this answer
 
Comments
VJ Reddy 13-May-12 7:44am    
Good answer. 5!
You can use the KeyPress event, and check for the correct characters, then args.Handled = true to filter the character: see http://www.programcall.com/36/csnet/example-for--winforms-keypress-event-in-dotnet.aspx[^]
 
Share this answer
 
Comments
VJ Reddy 13-May-12 7:44am    
Good point. 5!
One way is to use the Validating[^] event.
 
Share this answer
 
Comments
P.Salini 12-May-12 5:03am    
my 5!
Wendelius 12-May-12 5:07am    
Thanks :)
Sandeep Mewara 12-May-12 5:54am    
My 5!
Wendelius 12-May-12 6:04am    
Thanks :)
Maciej Los 12-May-12 18:41pm    
Good answer! +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