Scrolling a Panel in a Form

J

james_c

Hi,

I have a bunch of LabelFields on a Panel. The Panel's parent is a Form.
The Panel has the AutoScroll property set to true, and when the app. is run,
the vertical scrollbar is displayed. However, I am not able to scroll the
Panel using the up/down keys of the DPAD. Anyone know how I can make this
work?

My initial solution was to hide invisible TextBoxes at various locations
(top to bottom) on the panel with Tabindexes set, and then adding a KeyDown
event for each to go to the next tab index. But this solution sucks as the
scrolling is not smooth, etc. Basically, I want the same vertical scrolling
functionality I would get for free if I had added my labels directly to the
form.

Thanks in advance for any help!
 
A

Arun

Here is a snippet to do the same,

//pnl is your Panel with all your labels
//Override the OnKeyDown in your form and you can have any value other
than 30 to have a smooth transition.

protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Down:
{
// Down
pnl.AutoScrollPosition = new
Point(pnl.AutoScrollPosition.X, -
pnl.AutoScrollPosition.Y + 30);
e.Handled = true;
break;
}
case Keys.Up:
{
// Up
pnl.AutoScrollPosition = new
Point(pnl.AutoScrollPosition.X, -
pnl.AutoScrollPosition.Y - 30);
e.Handled = true;
break;
}
default:
break;
}
}

Hope this helps,
Cheers,
Arun
 

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