0

I have the following code:

private void txtNR_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
    {
        e.Handled = true;
    }
    else 
    {
    }

    // only allow one decimal point
    if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
    {
        e.Handled = true;
    }
    else
    {
        MessageBox.Show("You cannot type letters!"); 
    }
}

My question is: when I am trying to type letters the warning message is coming up front but the same is happening when i'm trying to type numbers, and after i click ok on message, the number is writtend down inside. Can you help me understand why?

Black Frog
  • 11,595
  • 1
  • 35
  • 66
cdrrr
  • 1,138
  • 4
  • 13
  • 44
  • can you explain which warning you are getting? – Gwny Nov 26 '14 at 14:15
  • Is not a warning, is the messagebox.show message – cdrrr Nov 26 '14 at 14:15
  • 1
    Are you trying to validate `double` or what? Displaying modal window *while typing* is pretty nasty thing to do. – Sinatr Nov 26 '14 at 14:16
  • What i want is: when the user is trying to type letter the messagebox.show message to appear, and when is typing number not to. – cdrrr Nov 26 '14 at 14:17
  • if `e.KeyChar` is not `.` then the statement is false - so you get `MessageBox.Show` – Milan Halada Nov 26 '14 at 14:18
  • Your "only allow one decimal" code is wrong. It will also show the message if there is only one decimal (because you're looking for any `IndexOf` > -1). If you display a messagebox while your user is trying to type text, they're going to stop using your app very quickly - it's a tremendously bad idea from a UI standpoint. As far as your question, there are dozens of previous questions here about how to properly restrict a TextBox to only accepting numeric values - why are you reinventing the wheel instead of using one of the working answers to any of those questions? – Ken White Nov 26 '14 at 14:20

5 Answers5

1

Your code should be like that:

if (!(char.IsControl(e.KeyChar) || char.IsDigit(e.KeyChar) || (e.KeyChar == '.')))
{
    e.Handled = true;
    MessageBox.Show("You cannot type letters!");
}
iBener
  • 469
  • 6
  • 18
0

Replace

else { }

with

else

Because of extra {} the second if statement is executed even if first one is handled. Due to this you are seeing message box even if the character is digit (which is already handled)

Tilak
  • 30,108
  • 19
  • 83
  • 131
0

Your condition is alright. You just need to set the Char to nothing

Try this:

 // only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
    e.Handled = true;
}
else
{
    MessageBox.Show("You cannot type letters!"); 
    e.KeyChar = '\0';
}
Horius
  • 119
  • 2
  • 17
0

I would suggest you to validate the content of the textbox after the user do his input ont it. Like @Sinatr said, for the user this could be an annoying thing to show a messagebox every time he writes wrong input.

It should also simplify your code, I think.

If the text is not numeric, then display the messagebox, something like that.

textbox_Validating(){
decimal d;
if(decimal.TryParse(textBox1.Text, out d))
{
    //valid 
}
else
{
    //invalid
    MessageBox.Show("Please enter a valid number");
    return;
}
}

Something like that... Sorry for the eventual errors in the code. Good luck.

SergioR
  • 48
  • 7
  • In this link you have a nice answer: http://stackoverflow.com/questions/18449388/check-if-textbox-input-is-a-decimal-number-or-not-c-sharp – SergioR Nov 26 '14 at 15:07
0

You don't say what your overall aim is.

However to correctly handle the event you are after this will work. You don't really want a pop up message all the time, better validate on form submission.

Are you trying to make a decimal text box ?

You've also got to take into account copy and paste which these events just aren't going to cover.

 if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
 {
    e.Handled = true;
 }
 else
 {   // only allow one decimal point
    if ((e.KeyChar == '.'))
    {
        if (((TextBox) sender).Text.Contains("."))
        {
            e.Handled = true;
        }
    }
    else
    {
        if (char.IsControl(e.KeyChar))
        {
            return;
        }

        if (!char.IsDigit(e.KeyChar))
        {
            MessageBox.Show("You cannot type letters!");
        }
    }
 }
krystan honour
  • 6,523
  • 3
  • 36
  • 63