Sign in to follow this  

[.net] Custom Scrolling Behavior in C#

Recommended Posts

I've been trying to write a custom scrollbar for a panel in C#. The panel has buttons on it that should be moved by a certain increment when the form is scrolled. I've been working on a custom scroll event handler but right now the scroll bar doesn't do a thing. I don't know how to offset or translate the panel or the controls with this scrollbar, and I've been searching for the answers to this for a while. Can anyone enlighten me? Thanks

Share this post

Link to post
Share on other sites

I wrote up a quick scrollbar demo and you can see the code here. For the demo I just dropped a panel onto a form, then dropped a scrollbar onto the panel. You could use a custom panel as well if you prefer, the behavior is the same. After that, I create a handler for the scrollbar's 'Scroll' event. Essentially, whenever the "scroll" event fires I check the new value of VScrollBar.Value. Based on that value of "Value" I re-position my button within the panel. Now, my example is contrived, and I dont take into acount the height of the button itself, but I think this will serve as a reasonable example to help you get started. In reality, you'd want to make sure to suspend the layout of your panel, etc...before moving the controls around to help prevent flickering.

// This is the code for the scrollbar in the designer generated code, showing the event handler for .Scroll

// vScrollBar1
this.vScrollBar1.Dock = System.Windows.Forms.DockStyle.Right;
this.vScrollBar1.Location = new System.Drawing.Point(249, 0);
this.vScrollBar1.Name = "vScrollBar1";
this.vScrollBar1.Size = new System.Drawing.Size(17, 240);
this.vScrollBar1.TabIndex = 0;
this.vScrollBar1.Value = 50;
this.vScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.vScrollBar1_Scroll);

// Here is my event handler

private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
// Get the new value of the scrollbar in the range of 0-50
int scrollValue = vScrollBar1.Value;

// convert scrollvalue into a percent
float percent = scrollValue * 0.01f;

// invert the percentage for the scrolling direction
percent = 1.0f - percent;

// multiply the percent by the height of the panel
int newHeight = (int)(panel1.Height * percent);

// Set the position of the button based on the new height
button1.Location = new Point(button1.Location.X, newHeight);

My little demo looks like this:


[Edited by - jwalsh on May 21, 2006 7:55:02 PM]

Share this post

Link to post
Share on other sites
Alternatively you can use the ScrollableControl.AutoScroll stuff which handles scrolling and clipping child controls just like this.

Panel derives from ScrollableControl.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this