Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Hide the Vertical Scrollbar While the Textbox is not Full and Display When it is Full

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

like image 867
Behrouz Hosseini K. Avatar asked Oct 15 '25 09:10

Behrouz Hosseini K.


2 Answers

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;
}
like image 149
KD82 Avatar answered Oct 16 '25 23:10

KD82


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.

like image 39
Daniel Möller Avatar answered Oct 17 '25 00:10

Daniel Möller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!