Click here to Skip to main content
15,888,195 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,I am a beginner of programming.I want to know how the HscrollBar works in a panel when the panel's autoscroll property is false in order to find other controls on the panel. I need sample codes to study.

Thank you very much.
Posted

1 solution

Hello,

Here's a basic exercise with sample code:

1. Add a Hscrollbar to a panel in your windows-forms project.

2. In the properties window (get it open from "View" if you don't have it open), change the "dock" property to "Bottom". This step is not essential.

2. Add a button to your panel on the left side of it.

3. Click the button with your mouse, then use the left arrow key on your keyboard to move it slightly over the left edge of the panel, so that half of the button is visible and half isn't.

4. Now for the sample code! Double click the horizontal scroll bar. you should see this:
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{

}

Now change that to this:
C#
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
    foreach (Control c in panel1.Controls)
    {
        c.Left += e.NewValue - e.OldValue;
    }
}

Code explanation: For each control in the panel you made, the computer changes the left margin of the control by the change in the scroll bar.

Hit F5 on your keyboard and play with the result!

I recommend you play with this code a bit to perfect it. I admit I haven't perfected its behaviour ;-). If you find this helpful, I'd be happy to know :-)
 
Share this answer
 
v2
Comments
kobayashi2013 19-Jan-13 2:49am    
Hello,Mr.Craig.Thank you for your good support!.I tried it,the button can be scrolled exactly.but it can not be back.if it possible,I want to set the botton on right outside of the form,and it can scroll freely(move and move back).Do you know the mechanism?
Mitchell J. 19-Jan-13 5:15am    
I'm not quite sure what you mean. I still highly recommend you play with the code and try guess-and-error to achieve what you want.

Suggestions for your playing:
1. Try multiplying the effect on controls. ie, change "e.NewValue - e.OldValue" to "3 * (e.NewValue - e.OldValue)".
2. Add heaps of controls to your panel and obseve what happens as you add more of them.

I've self-taught myself programming and I highly recommend you spend time perusing other people's work here on CP. I'm glad I was of help! :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900