Can you please let me know how I can set the windows form textbox vertical scroller in a mood that Scrollbar displays only when the Text size is more that the space of textbox?
Thanks
Or if your textBox1 is multiline and contains e.g. 20 lines:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Split('\n').Length > 20)
textBox1.ScrollBars = ScrollBars.Vertical;
else
textBox1.ScrollBars = ScrollBars.None;
}
One way I can think is to set the font of the text box to one of those having the same width for all characters, such as Lucida Console
.
Then you measure how many characters you need to hit the end of the text box.
So, knowing that number, add to the TextChanged
event a method to set scroll bar only if text has more than the maximum number.
private void textBox1_TextChanged(object sender, EventArgs e)
{
int MaxChars = 10; //suppose that's the maximum
if (textBox1.Text.Count() > MaxChars)
textBox1.ScrollBars = ScrollBars.Vertical;
else
textBox1.ScrollBars = ScrollBars.None;
}
You can also calculate MaxChars
with some kind of:
double param1 = figure out this number;
double param2 = figure out this number too;
int MaxChars = (int)(Math.Floor(param1*textBox1.Width - param2));
This way you can resize the component dinamically.
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