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");
}
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.
private void num1Button_Click(object sender, EventArgs e)
{
if (richtextbox.Text.Length >= 6)
return;
richtextbox.Text = richtextbox.Text.Insert(0, "1");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With