0

I create a Form in Visual Studio that contains many fields to display; the fields need extra area (larger than screen size).
I am trying to resize the Form but it prevents me from doing it.
I tried to add a vertical Scrollbar but also didn't work.

Note that I want to add a new Scrollbar to my Form and change its color, not activate the default Scrollbars.

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);
 }
Sarah
  • 21
  • 5
  • Build a CustomControl, using the [ScrollBarRenderer](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.scrollbarrenderer) class to draw the ScrollBar's parts. – Jimi Dec 30 '19 at 14:09
  • If it's not enough, you have to build a control from scratch, or derive one from (or be *inspired* by) the [Scrollbar control - .Net Source Code](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ScrollBar.cs,36). See also the [VisualstyleElement.ScrollBar](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/VisualStyles/VisualStyleElement.cs,1097). If it's not enough, you probably have to move to WPF. – Jimi Dec 30 '19 at 14:25
  • What are you targetting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! – TaW Dec 30 '19 at 16:15

1 Answers1

1

You can use the following code to add a vertical Scrollbar successfully.

 ScrollBar vScrollBar1 = new VScrollBar();
    private void Form1_Load(object sender, EventArgs e)
    {
        vScrollBar1.Dock = DockStyle.Right;
        vScrollBar1.Dock = DockStyle.Right;
        vScrollBar1.Scroll += new ScrollEventHandler(vScroller_Scroll);
        panel1.Controls.Add(vScrollBar1);
        panel1.VerticalScroll.Visible = false;
        panel1.VerticalScroll.Enabled = true;
        this.Controls.Add(vScrollBar1);

    }
    private void vScroller_Scroll(object sender, ScrollEventArgs e)
    {
        panel1.VerticalScroll.Value = e.NewValue;
    }

As for failure for changing the color, you can look at the Vertical Scrollbar color does not change. It describes the reason clearly.

Result: enter image description here

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27