Answered by:
TextBox MaxLength issue - BEGINNER

Question
-
This is a C# windows form based application, i am using VS2008.
I have a textbox, and i enabled MultiLine to true. Now i need this textbox to include a maximum of 100 characters. So i set the MaxLength property to 100. But yet i am able to enter more than 100 characters. What should i do ?
--------------------------------------------------------------------------------------------
I tried this again, and it works.
But, according to my scenario it doesn't.
I have a form, and a text box and a button on it. When i click the button, a character is printed on the textbox. (Like the calculator, you click a button, and the digit gets printed on the textbox).
I need to restrict the number of characters in the textbox to 100. and i am not clicking the textbox, and entering values via the keyboard. I will be entering values by clicking the button.
(sorry i didn't think this part was relevent)
Monday, June 27, 2011 12:37 PM
Answers
-
What about trying something else:
int textLimit; public Form1() { InitializeComponent(); textBox1.MaxLength = 10; textLimit = textBox1.MaxLength; } private void button1_Click(object sender, EventArgs e) { string text = "This is too long text."; if (text.Length < textLimit) textBox1.Text = text; else MessageBox.Show("The text you want to insert into textbox1 is to long."); }
Mitja- Marked as answer by LocalP Monday, June 27, 2011 1:31 PM
Monday, June 27, 2011 1:30 PM
All replies
-
Hello LocalP,
this code works:
this.textBox1.MaxLength = 5; this.textBox1.Multiline = true;
Maybe you modify different textBox control.Regards
Adam
Monday, June 27, 2011 12:43 PM -
I reproduced same thing in my code and I am NOT able to write more than 100 once I set the MaxLength is 100. Most probably you might have set the Maxlength property to different text box instead of the one you wanted to set. (By default the maxlength is 32767
Dreaming a world without any war in anywhereMonday, June 27, 2011 12:46 PM -
I tried this again, and it works.
But, according to my scenario it doesn't.
I have a form, and a text box and a button on it. When i click the button, a character is printed on the textbox. (Like the calculator, you click a button, and the digit gets printed on the textbox).
I need to restrict the number of characters in the textbox to 100. and i am not clicking the textbox, and entering values via the keyboard. I will be entering values by clicking the button.
(sorry i didn't think this part was relevent)
Monday, June 27, 2011 12:57 PM -
What about trying something else:
int textLimit; public Form1() { InitializeComponent(); textBox1.MaxLength = 10; textLimit = textBox1.MaxLength; } private void button1_Click(object sender, EventArgs e) { string text = "This is too long text."; if (text.Length < textLimit) textBox1.Text = text; else MessageBox.Show("The text you want to insert into textbox1 is to long."); }
Mitja- Marked as answer by LocalP Monday, June 27, 2011 1:31 PM
Monday, June 27, 2011 1:30 PM -
Changed to code a bit (in upper post). Now it correct.
MitjaMonday, June 27, 2011 1:33 PM