开发者

How to avoid/warn user for wrong entries in Windows Application

How I can tell the user by showing a MSG box开发者_StackOverflow社区 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


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


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
}


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜