baban jamal baban jamal - 1 year ago 63
C# Question

How to limit the textbox input charachters count?

When I get my specific data from a

and I want to put it in
, I want the
to just accept 11 characters, not more.

How do I achieve that?

textBox3.MaxLength = 11;
textBox3.Text= dataGridView1.CurrentRow.Cells["Phone Number"].Value.ToString();

Answer Source

Use SubString to truncate the string.

// Set the MaxLength, may not be needed if the field cannot be altered manually
textBox3.MaxLength = 11;

// Get the text value
var text = dataGridView1.CurrentRow.Cells["Phone Number"].Value.ToString();

// Starting from the first character, take up to 11 characters max
textBox3.Text = text.Substring(0, text.Length > 11 ? 11 : text.Length);


string longString = "abcdefghijklmnop";
string shortString = "abcde";


longString would show abcdefghijk
shortString would show abcde

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download