RE: Auto Scroll Control on Panels

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"HaySeed" wrote:

I have a Panel which is being used as a paint surface. When the user presses
the up/down arrows or Page Up/ Down buttons I would like to scroll the panel
according to the proper increment.

Questions:
1) How do I get a handle to the AutoScroll Scrollbars when they are visible?
2) How do I increment the scrollbar (N) pixels down the panel?


I have tried the following Win32 call -

private const int WM_HSCROLL = 276;
private const int WM_VSCROLL = 277;
private const int SB_LINEUP = 0;
private const int SB_LINEDOWN = 1;
private const int SB_PAGEUP = 2;
private const int SB_PAGEDOWN = 3;
private const int SB_PAGETOP = 6;
private const int SB_PAGEBOTTOM = 7;
private const int SB_ENDSCROLL = 8;

SendMessage(panel.Handle, WM_VSCROLL, (IntPtr)SB_LINEDOWN,
IntPtr.Zero);

This does jump the scroll bar as needed but I could not find how to control
the distances the scrollbar would travel for a Line/Page/Top/Bottom etc.

Any thoughts would be appreciated.


Hi HaySeed,

You might find it easier if you just put the drawing-panel inside another
scrollable control and have the container control handle the scrolling. To
trap key events in your drawing-panel you need to create your own panel and
set Focus at some point, like at the MouseDown event. The code below
demonstrates how you can handle arrow key navigation in a panel. panel1 is
the fixed size control and will add scrollbars as needed. panel2 is the
drawing panel and can be larger than panel1. Note, the code is jotted down
quickly without regard for good design etc so you might want to tweak it
quite a bit and possibly look for better solutions.

public partial class Form1 : Form
{
public class MyPanel : Panel
{
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
}

protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);

Focus();
}
}

Panel panel1;
MyPanel panel2;

public Form1()
{
panel1 = new Panel();
panel1.Size = new Size(100, 100);
panel1.Location = new Point(10, 10);
panel1.AutoScroll = true;
panel1.BorderStyle = BorderStyle.FixedSingle;
Controls.Add(panel1);
panel2 = new MyPanel();
panel2.Size = new Size(300, 300);
panel2.Location = new Point(0, 0);
panel1.Controls.Add(panel2);

panel2.MouseDown += panel2_MouseDown;
panel2.Paint += panel2_Paint;
panel2.KeyDown += panel2_KeyDown;
}

void panel2_KeyDown(object sender, KeyEventArgs e)
{
int newvalue = 0;
switch (e.KeyCode)
{
case Keys.Left:
newvalue = panel1.HorizontalScroll.Value
+ panel1.HorizontalScroll.SmallChange;
break;
case Keys.Right:
newvalue = panel1.HorizontalScroll.Value
- panel1.HorizontalScroll.SmallChange;
break;
}

if (newvalue < panel1.HorizontalScroll.Minimum)
newvalue = panel1.HorizontalScroll.Minimum;
else if (newvalue > panel1.HorizontalScroll.Maximum)
newvalue = panel1.HorizontalScroll.Maximum;

panel1.HorizontalScroll.Value = newvalue;
}

void panel2_Paint(object sender, PaintEventArgs e)
{
foreach (Point p in points)
e.Graphics.DrawEllipse(Pens.Red, new Rectangle(p.X - 5, p.Y - 5,
10, 10));
}

List<Point> points = new List<Point>();

void panel2_MouseDown(object sender, MouseEventArgs e)
{
points.Add(e.Location);
panel2.Invalidate();
}
}



--
Happy Coding!
Morten Wennevik [C# MVP]
.



Relevant Pages

  • Auto Scroll Control on Panels
    ... I have a Panel which is being used as a paint surface. ... according to the proper increment. ... private const int WM_HSCROLL = 276; ... This does jump the scroll bar as needed but I could not find how to control ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Using autoscroll
    ... >> selectively scroll objects within your panel, ... >> You have three objects in the panel - A, B, and C. ... > this on a panel using its autoscroll properties, ... >> apply the scrolling translation to the selected objects. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Using autoscroll
    ... You can certainly use the AutoScroll capabilities of the Panel control to do ... write some translation code within your Paint event. ... >> selectively scroll objects within your panel, ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: InputPanel Class Question
    ... At the right of the panel, add a vertical scroll bar, set it to invisible, set its height to the space above the SIP and set the min value to 0 and the maximum to the SIP height. ... When you set the scrollbar to visible, also set it's value to the SIP height, so that the panel top is moved up, and the textbox displayed, and when it is hidden, set the scrollbar value to 0 so the panel is moved to its original position. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Autoscroll woes
    ... I have 2 rich textboxes within an auto-scrolling panel object. ... The panel needs to scroll primarily to accomodate textbox2, ... The problem I'm experiencing is that when the user is typing into textbox2, ... the .top of the currently focused textbox is outside the viewable area, ...
    (microsoft.public.dotnet.languages.vb)