user2788405
user2788405

Reputation: 325

How to add scrollbars in c# form

For some reason only adding a vertical scroll bar works with my code.

I can't seem to add BOTH a vertical and horizontal scroll bar.

private void textBox1_TextChanged(object sender, EventArgs e)
{
    textBox1.ScrollBars = ScrollBars.Vertical;     
}

Upvotes: 12

Views: 43995

Answers (5)

Bob
Bob

Reputation: 1

Drag and drop vertical or horizontal scroll bars from ToolBox onto the form. Dock to right and bottom.

Alternatively code location and size in Form_Load and Form_Resize. Another way is to use GDI32. If GDI32 is used you don't need to add scroll bars from the ToolBox or programmatically. Call SetScrollRange and SetScrollPos in Form_Load and Form_Resize. Scrollbar attached to form will appear automatically. Suggest using SetScrollInfo in GDI32 to create proportional scrollbar. If you do not call SetScrollInfo a non-proportional scrollbar will be created. This is a scrollbar where the thumb size stays constant as window resizes.

Upvotes: -1

Mlarnt90
Mlarnt90

Reputation: 186

you don't need to write a code for this. Just change the properties of textBox. For both scroll bars, if Multiline set to True, then set ScrollBars to Both and set WordWrap to False in properties. No need for writing code at all since this is for WinForms.

Upvotes: 3

Pir Fahim Shah
Pir Fahim Shah

Reputation: 10633

If you want to add Vertical ScrollBar in your Form. Then copy and paste this code in Form LOAD EVENT. like

private void Form1_Load(object sender, EventArgs e)
    {
        VScrollBar vScroller = new VScrollBar();
        vScroller.Dock = DockStyle.Right;
        vScroller.Width = 30;
        vScroller.Height = 200;
        vScroller.Name = "VScrollBar1";
        this.Controls.Add(vScroller);
     }

Upvotes: 0

King King
King King

Reputation: 63387

You have to set both ScrollBars and WordWrap like this:

textBox1.ScrollBars = ScrollBars.Both;
textBox1.WordWrap = false;

NOTE: All the settings above are done 1 time. No need to place the code in the TextChanged event handler.

Upvotes: 21

User1000547
User1000547

Reputation: 4311

private void textBox1_TextChanged(object sender, EventArgs e)
{
    textBox1.ScrollBars = ScrollBars.Both;     
}

ScrollBars.[Value] is an enum: Valid values are Horizontal, Vertical, None, and Both.

Upvotes: 2

Related Questions