0

How I can tell the user by showing a MSG box that what he enterd is not in the right format I have textbox that i want to get only numbers and if the user entered letters instead to show him a msg

int level;
        if(range.Text.GetType)
        {
            MessageBox.Show("Please Enter Only Number");
        }
        else
        {
        level = int.Parse(range.Text);
        // use the randomGenerator function according to the number i entered.
        TheRandNo = randomGenerator.Next(level);
        //Activate the guess button.
        GuessBT.Enabled = true;
        label4.Text = x.ToString();
        // To start the game with the green color
        BackColor = Color.Green;
        }

thanx in advance

Tony
  • 487
  • 3
  • 9
  • 12

4 Answers4

4

You may want to use a MaskedTextBox instead, which only allows specific characters (in this case, numbers) to be entered.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
1
if(!int.TryParse(range.Text, out level))
{
  MessageBox.Show("Please Enter Only Number");
}
else
{
  // No need for your Parse now, level has the right value already
}
Benjamin Podszun
  • 9,679
  • 3
  • 34
  • 45
  • can you show me how to use the tryParse without passing the (out level) because its not lways there ... – Tony Jan 04 '10 at 15:14
  • You can't. The whole idea of the .Net Framework's Try* methods (TryParse, TryGetValue for example) is to return a bool that says if the try succeeded. The result is returned in an out parameter. You have two options here. a) You want to use the value anyway (then do it like I wrote above) b) You only want to know if this is an int. In that case you can either go for Bruno's first solution or - define a local int that you don't use and stick to int.TryParse(). – Benjamin Podszun Jan 04 '10 at 15:35
0

Something like:

if (!range.Text.All(c =>  char.IsDigit(c)))
   MessageBox.Show("Please Enter Only Number");

or alternately you could use int.TryParse:

int level;
if(!int.TryParse(range.Text, out level))
{
   MessageBox.Show("Please Enter Only Number");
}
else 
{
   // ...
}

This depends on the kind of number that you need to parse.

bruno conde
  • 47,767
  • 15
  • 98
  • 117
  • can you show me how to use the tryParse without passing the (out level) because its not lways there ... – Tony Jan 04 '10 at 15:14
0

Personally, I'd go with R. Bemrose's suggestion above to use a MaskedTextBox; maybe I'd put a label near it (or show a label whenever the TextBox became active and had focus) telling the user to enter numbers only.

But ... if you are "determined" to show the end-user a message, let me suggest an alternative to 'MessageBox which is much less "disruptive" to the flow of the application, imho.

Consider that using a MaskedTextBox you have access to events like :

private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
    Console.WriteLine("rejected char");
}

private void maskedTextBox1_TextChanged(object sender, EventArgs e)
{
    Console.WriteLine("accepted char");
}

When the user enters a character that doesn't meet the criteria defined by the Mask : the MaskInputRejected event will fire : you could show a label if that happens, for example, as an alternative to MessageBox.

You could even maintain a counter variable of how many times the MaskInputRejected event has been called in the "current session," and put up an "ominous" MessageBox :) if the user has entered invalid characters a certain number of times.

When a valid character is entered, in your case a number, the TextChanged event is triggered : you could then hide the label.

If this is a "game," you could do something with a "winking blinking label" with a transparent background (use a Timer or something to make it wink or blink).

For any given UI design, imho, there is no "one size fits all" answer, but I would urge you, if you are a newcomer to WinForms .NET, and its controls, to spend some time studying and using the MaskedTextBox which is really a very handy control with many features.

Just in the odd case you are working in WPF here (if so, please add that tag to your question) see : SO thread on WPF and MaskedTextBox

Community
  • 1
  • 1
BillW
  • 3,415
  • 4
  • 27
  • 46