1
votes

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!

3
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γηράσκω δ' αεί πολλά διδασκόμε
Can you explain why you need those manually added scroll bars? You can get a panel to scroll by setting AutoScroll property.nevets
I tried this already, drag and drop the scroll bars, change the AutoScroll to true, but it's not workinguser2922456
Delete your scrollbar controls and just set the container's AutoScroll property to true.LarsTech
I have been tried this, just setting AutoScroll = true..still dose'nt worksuser2922456

3 Answers

2
votes

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..

0
votes

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

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