简体   繁体   中英

How do I make a richtextbox limited to only digits with a max number? (Cap)

Hey guys I wanted to make a richtextbox that only supports numbers and cant go above, 500 for example.

how would I go by doing that? thanks

I'm not sure about the specifics but you can add something like

 myRichTextBox.OnTextChanged() {
    int number = 0;
    bool checkInt = Int32.TryParse(myRichTextBox.Text, out number); //this checks if the value is int and stores as true or false, it stores the integer value in variable "number"
    if ( checkInt =  true && number > 500  ) //check if value in textbox is integer

        {

            myRichTextBox.Text = number.ToString();
        }
    else 
    {
        DialogBox.Show("Please Enter Numbers Only");
        myRichTextBox.Text = "";

    }
}

You probably have to read the Int32.TryParse usage but brushing up this code should do what you want.

You can also put this code in a button onclick method to check that the value in textbox is integer before using the text.

I would use the keydown event to check if the pressed key is one of the keys you allow. With numbers it is pretty simple, maybe add ',' and '.' or other characters of your choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM