Behrouz Hosseini K.
Behrouz Hosseini K.

Reputation: 135

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

Upvotes: 4

Views: 10324

Answers (6)

Emre
Emre

Reputation: 97

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

Upvotes: 1

Blackmeser
Blackmeser

Reputation: 99

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

Upvotes: 0

Colby Africa
Colby Africa

Reputation: 1376

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

Upvotes: 2

Daniel Möller
Daniel Möller

Reputation: 86600

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.

Upvotes: 2

KD82
KD82

Reputation: 81

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

Upvotes: 3

Kaveh Shahbazian
Kaveh Shahbazian

Reputation: 13513

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>

Upvotes: 0

Related Questions