Hi guys

Having a little problem here. Please help, Im trying to have a MessageBox appear when a user reaches the textbox.maxLength property

tried

if (textBox2.MaxLength < 10)
{
MessageBox.Show("Max characters reached");
}

Recommended Answers

All 7 Replies

MaxLength is the constant that you should compare against. I think the comparison would be textBox1.TextLength > textBox1.MaxLength, but I tried it a few times and it looks like you don't need to do anything because once you're at the MaxLength it won't let you add anything more into the textbox anyway.

I assume you are putting the code in the textboxes TextChanged event.
You are checking if the max length of the textbox is less than 10.
What you need to do is check if the length of the text entered is greater than the max length:

if (txtCustName.Text.Length > txtCustName.MaxLength)
            {
                 MessageBox.Show("Max characters reached");
            }

I was just going to add into my post hehe. If you want it to show the message box it has to be >= textBox.MaxLength (or just ==) because the box won't let you surpass the count.

Jonsca is right of course. If you wanted to show the messagebox to inform the user when they reached the max just change the comparison to txtCustName.Text.Length == txtCustName.MaxLength. That way the messagebox will show when they enter the last character.

You could also put the above code into the textboxes KeyPress event. That way if they tried to enter a character after the max was reached it would show the message so they knew why nothing appeared in the textbox.

Great minds think alike it would seem :p

commented: Synchrony in C# +2

Ryshad and Jonsca.. Thanks for the help. Much appreciated

No worries, remember to mark the thread as solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.