Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Richtextbox Max length

Tags:

c#

winforms

I have several controls in my windows form, namely a Richtextbox and 10 buttons which represents a numpad (from 0-9). When a button is clicked, it will insert the corresponding number into the Richtextbox. I have set the MaxLength property to 6, however i seem to be able to insert more than 6 characters into the textbox by hitting on the buttons. My code is as follows:

private void num1Button_Click(object sender, EventArgs e)
{
    richtextbox.Text = richtextbox.Text.Insert(0, "1");
}
like image 991
aurelio Avatar asked Jul 15 '26 14:07

aurelio


1 Answers

Description

MSDN - Gets or sets the maximum number of characters the user can type or paste into the rich text box control.

So you need to check the length in your code.

Sample

private void num1Button_Click(object sender, EventArgs e)
{
    if (richtextbox.Text.Length >= 6)
        return;
    richtextbox.Text = richtextbox.Text.Insert(0, "1");
}

More Information

  • MSDN - RichTextBox.MaxLength Property
like image 142
dknaack Avatar answered Jul 18 '26 03:07

dknaack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!