Jump to content
  • Advertisement
Sign in to follow this  

[.net] Custom Scrolling Behavior in C#

This topic is 4437 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!