spunit
spunit

Reputation: 565

TextBox with maxvalue

I want to make a TextBox which does not allow to enter a value above 100. Only numbers allowed, And a Numeric TextBox is not an option. This is my code for now:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) { e.Handled = true; }  // only numbers
}

Any ideas?

Upvotes: 2

Views: 24715

Answers (4)

Sultan
Sultan

Reputation: 33

Hello, here is my solution.



    private void textBox_KeyPress(object sender, KeyPressEventArgs e)
            {
                char c = e.KeyChar;
                if ((!char.IsDigit(c) ||
                    Convert.ToInt32(textBox.Text + e.KeyChar) >= 101 ||
                    textBox.Text == "0") && c != '\b')
                    e.Handled = true;

            }

Upvotes: 1

FeliceM
FeliceM

Reputation: 4199

You can enter only numbers and use arrows keys and backspace. If you enter a number > than 100 or less than 1, when you press enter it will be cancelled. Copy and Past with button key down is disabled and also mouse right click to prevent the user to paste in the text box is disabled/handled. This should solve your problem in full. First of all set:

    ShortcutsEnabled property of your text box to False

this will not allow mouse right click and ctrl+V for paste in your text box. Then add the following code:

    //prevent letters, special chars, punctuation, symbols, white spaces
    private void txtType1_KeyPress(object sender, KeyPressEventArgs e)
    {
        {
            if (char.IsLetter(e.KeyChar) ||
                char.IsSymbol(e.KeyChar) ||
                char.IsWhiteSpace(e.KeyChar) ||
                char.IsPunctuation(e.KeyChar))
                e.Handled = true;
        }

        {
            //allows only numbers between 1 and 100
            string value = txtType1.Text;
            if (txtType1.Text !="")

            {
                if (Int16.Parse(value) < 1 )
                {
                    txtType1.Text = ""; 
                }
                else if (Int16.Parse(value) > 100)
                {
                    txtType1.Text = "";
                }

                }
            }
        }

Upvotes: 0

spunit
spunit

Reputation: 565

Finally. I found a solution:

int box_int = 0; Int32.TryParse(textBox1.Text, out box_int);
if (box_int > 1050 && textBox1.Text != "") { textBox1.Text = "1050"; }

Upvotes: 0

Habib
Habib

Reputation: 223237

You should use int.TryParse to see if the parsing is successful and then compare the value to see if it is below 100.

int number;
if(int.TryParse(textBox1.Text, out number))
{
  if(number <= 100) 
    {
     //in range
    }
  else
    {
     // not in range
    }
}
else
{
//invalid number
}

You can also use double.TryParse or other TryParse method depending on the type, they are safe to use, since they will return a false if the parsing fails, instead of raising an exception.

Upvotes: 4

Related Questions