adding scrollbars in C# Forms 2.0 - howto here

R

raylopez99

Scrollbars scroll bars adding scrollbars in C# Forms 2.0 scrollbars
don't work won't work how to add scrollbar in C#

Here is how to add a scrollbar to a form. It's not in any book. For
future reference only, no question being asked.

This works for forms, as well as "panels".

Below example is for forms, but will also work in a panel.

RL

public MyForm()
{

InitializeComponent();
myRect = new Rectangle(150,150,10,5000); //this is what
you want to draw, but it's really long and won't fit in a normal
window, so add a scrollbar

//first, you *must* add a dummy control to your form (mandatory).
Here called button b
Button b = new Button();
b.Location = new Point(0, 5000); //place it at the lower
left corner
b.Text = "dummy_button"; //call it something (you can also
make it invisible but that's optional)

this.Controls.Add(b); //add to form
}

//now make sure you check the Properties checkbox for the form (MyForm
here) for "AutoScroll" to "true" in the Visual Studio Designer
(Wizard), which will add a line in the machine generated constructor

// now you have scrollbars added automatically.

//these below lines are to prevent flicker (also check DoubleBuffer
property in the form)

private void MyForm_Scroll(object sender, ScrollEventArgs e)
{
this.Invalidate(true);
}

private void MyForm_VisibleChanged(object sender, EventArgs e)
{
this.Invalidate(true);
}

private void MyForm_Resize(object sender, EventArgs e)
{
this.Invalidate(true);


// more here: http://www.bobpowell.net/understanding_autoscroll.htm //
if you want your graphic to 'move' with the scrollbar--note how the
matrix is used --I find this is nice but optional
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top