1

I need to add scroll horizontal and vertical scroll bar. The problem is that they doesn't work, as in when I use them the screen doesn't move.

VScrollBar vScrollBar1 = new VScrollBar();
HScrollBar hScrollBar1 = new HScrollBar();

vScrollBar1.Dock = DockStyle.Left;
hScrollBar1.Dock = DockStyle.Bottom;

Controls.Add(vScrollBar1);
Controls.Add(hScrollBar1);

I use the code to add scroll bars, how do I activate them or get them to work as I need?

Thanks!

6
  • 1
    You are adding controls. These controls have events. You have to write code to actually do something. It is like adding a button and say i click it and nothing happens. If you dont bother dont create vertical and horizontal scrollbars. Just set true to AutoScroll property of your Form Commented Sep 16, 2014 at 14:37
  • Can you explain why you need those manually added scroll bars? You can get a panel to scroll by setting AutoScroll property. Commented Sep 16, 2014 at 14:46
  • I tried this already, drag and drop the scroll bars, change the AutoScroll to true, but it's not working Commented Sep 16, 2014 at 14:48
  • Delete your scrollbar controls and just set the container's AutoScroll property to true. Commented Sep 16, 2014 at 14:55
  • I have been tried this, just setting AutoScroll = true..still dose'nt works Commented Sep 16, 2014 at 15:00

3 Answers 3

2

You usually don't add Scrollbars; you set AutoScroll = true in the form's property panel.

Now when any control grows out of the Form or is moved over right or bottom border the Form will show the necessary Scrollbar.

You can test it with a Label and a TextBox: Set the Label to the right border and script the TextBox's TextChanged event like this:

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

Now run the programm and enter stuff into the Textbox; you will observe how the Label grows and how the Form brings up a horizontal Scrollbar when it goes over the edge.

Note 1: This will not work if the Form has AutoSize = true - then instead the form will grow! If the Form has both AutoSize and AutoScroll true, then AutoSize will win.

Note 2: This test will only work if the Label has AutoSize = true, as it has by default..

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this, the form autoScroll is true the autoSize is false..some labels get out of the form bcause of they size but scrollBar dosent brought up.. There are AutoScrollSize and AutoScrollMargin property, its matter what they eqauls to?
Their defaults should do. Is the label docked?
good. however there is something not right with your programm as it is. please try this out with a completely fresh form and you will see that the only things you need to do is set the forms autoscroll to true and place/grow a control over the border.. once you see that you can go back and search for the things that are wrong/different in your real form..
0

You need to use the Panel control as container of your child controls and set "AutoScroll" property to true.

Set true to AutoScroll property of Form.

Write this code in your Form Load Event, and you will get your scroll bar, like I am writing it here in my Form Load Event.

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

2 Comments

try to add a Panel container and use the snippet code i suggested
Dosent work..When some simple text is very long and get out of the frame the scroll dosent work
0

vScrollbars and hScrollbars are just plain controls without code. [UI]

You need to code to make them do something!

Or just set the property 'AutoScroll = true;' in your form or add a panel and set it to true.

However your control needs Focus() to scroll with your mouse wheel.

Here is a little workaround:

public Main()
{
    InitializeComponent();

    //Works for panels, richtextboxes, 3rd party etc..
    Application.AddMessageFilter(new ScrollableControls(panel1, richtextbox1, radScrollablePanel1.PanelContainer));
}

ScrollableControls.cs:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

//Let controls scroll without Focus();

namespace YOURNAMESPACE
{
    internal struct ScrollableControls : IMessageFilter
    {
        private const int WmMousewheel = 0x020A;
        private readonly Control[] _controls;

        public ScrollableControls(params Control[] controls)
        {
            _controls = controls;
        }

        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            if (m.Msg != WmMousewheel) return false;
            foreach (var item in _controls)
            {
                ScrollControl(item, ref m);
            }
            return false;
        }

        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        private static void ScrollControl(Control control, ref Message m)
        {
            if (control.RectangleToScreen(control.ClientRectangle).Contains(Cursor.Position) && control.Visible)
            {
                SendMessage(control.Handle, m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32());
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.