c#: By default text box accepts n number of entries,
I want to restrict the entries to its width
Is there any property in text box through which i can achive this,
c#: By default text box accepts n number of entries,
I want to restrict the entries to its width
Is there any property in text box through which i can achive this,
No, to do this you will have to calculate the maximium number of characters they can enter by the text box width manually I'm afraid. You'll also have to take into consideration the font and font size too
default it accepts only 32767
of chars.
You can set the MaxLength
property of the textbox in the textbox property
Hope you are using windows forms
Assuming WinForms, try this:
private bool textExceedsWidth;
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
textExceedsWidth = false;
if (e.KeyCode == Keys.Back)
return;
Size textSize = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
if (textBox1.Width < textSize.Width)
textExceedsWith = true;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (textExceedsWidth)
e.Handled = true;
}