简体   繁体   中英

Adding scroll bar to windows forms, c#

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!

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM