'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



Solution 1:[1]

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.

Solution 2:[2]

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;
}

Solution 3:[3]

Here is another approach:

internal static Size GetTextDimensions(Control control, Font font, string stringData)
{
    using (Graphics g = control.CreateGraphics())
    {
        SizeF sizeF = g.MeasureString(stringData, font);
        return new Size((int)Math.Ceiling(sizeF.Width), (int)Math.Ceiling(sizeF.Height));
    }
}

Usuage:

        Size dimensions = ControlManager.GetTextDimensions(descriptionTextBox, descriptionTextBox.Font, descriptionTextBox.Text);

        descriptionTextBox.ScrollBars = dimensions.Height > 
                                        descriptionTextBox.Height ? 
                                        ScrollBars.Vertical : ScrollBars.None;

You may have to add or subtract depending on padding, but this works great. Here is an extension method version:

    public static Size GetTextDimensions(this Control control, Font font, string stringData)
    {
        using (Graphics g = control.CreateGraphics())
        {
            SizeF sizeF = g.MeasureString(stringData, font);
            return new Size((int)Math.Ceiling(sizeF.Width), (int)Math.Ceiling(sizeF.Height));
        }
    }

Solution 4:[4]

 private void textBox_TextChanged(object sender, EventArgs e)
    {
        if (textBox.Multiline)
        {
            textBox.ScrollBars = textBox.Text.Length > (textBox.Width + textBox.Height) / 1.30f ? ScrollBars.Vertical : textBox.Text.Split('\n').Length > textBox.Height / textBox.Font.Size/1.4f ? ScrollBars.Vertical : ScrollBars.None;
        }
    }

Solution 5:[5]

You may wrap the TextBox inside a ScrollViewer and set VerticalScrollBarVisibility="Auto" for ScrollViewer. This code works (tested on Visual Studio 2012 & .NET 4.5):

<StackPanel>
    <ScrollViewer Height="100" VerticalScrollBarVisibility="Auto">
        <TextBox TextWrapping="Wrap" 
                    ScrollViewer.VerticalScrollBarVisibility="Auto" 
                    ScrollViewer.HorizontalScrollBarVisibility="Hidden">
        </TextBox>
    </ScrollViewer>
</StackPanel>

Solution 6:[6]

Upgrade method by Colby Africa

public static Size GetTextDimensions(this TextBox textBox)
{
    Font font = textBox.Font;
    string stringData = textBox.Text;
    int width = textBox.Width;
    using (Graphics g = textBox.CreateGraphics())
    {
        SizeF sizeF = g.MeasureString(stringData, font, width);
        return new Size((int)Math.Ceiling(sizeF.Width), (int)Math.Ceiling(sizeF.Height));
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 KD82
Solution 3 Colby Africa
Solution 4
Solution 5
Solution 6 Blackmeser