Advertisement:

Skystone Software

http://www.SkystoneSoftware.com

Scott Waletzko's Blog
AutoScroll and Key Events
Published: 3/4/2008
XMl / RSS

To the best of my knowledge, the .NET Panel control doesn't support the use of keystrokes to change the scroll position. This is likely because of the confusion that would cause with potential controls on the panel that support their own scrolling operations (ListView, TreeView, etc). You can implement this behavior, however, by handling the KeyUp event of the form on which your panel is located. I did find some strange buggy behavior in this implementation for which I had to come up with a workaround - here's the code I ended up using as an example within an answer to a post over at the VBCity forums.

VB:
Public Partial Class Form1
	Inherits Form

	Public Sub New()
		InitializeComponent()
		Me.panel1.AutoScroll = True
	End Sub

	Private m_ptAutoScroll As System.Drawing.Point = New Point(0, 0)

	Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
		Try
			If e.KeyCode = Keys.Up Then
				m_ptAutoScroll = New Point(m_ptAutoScroll.X, m_ptAutoScroll.Y - 1)
			ElseIf e.KeyCode = Keys.Down Then
				m_ptAutoScroll = New Point(m_ptAutoScroll.X, m_ptAutoScroll.Y + 1)
			ElseIf e.KeyCode = Keys.Left Then
				m_ptAutoScroll = New Point(m_ptAutoScroll.X - 1, m_ptAutoScroll.Y)
			ElseIf e.KeyCode = Keys.Right Then
				m_ptAutoScroll = New Point(m_ptAutoScroll.X + 1, m_ptAutoScroll.Y)
			ElseIf e.KeyCode = Keys.PageDown Then
				m_ptAutoScroll = New Point(m_ptAutoScroll.X, m_ptAutoScroll.Y + 10)
			ElseIf e.KeyCode = Keys.PageUp Then
				m_ptAutoScroll = New Point(m_ptAutoScroll.X, m_ptAutoScroll.Y - 10)
			ElseIf e.KeyCode = Keys.Home Then
				m_ptAutoScroll = New Point(m_ptAutoScroll.X - 10, m_ptAutoScroll.Y)
			ElseIf e.KeyCode = Keys.End Then
				m_ptAutoScroll = New Point(m_ptAutoScroll.X + 10, m_ptAutoScroll.Y)
			End If
			Me.panel1.Invalidate()
		Catch
		End Try
	End Sub

	Private Sub panel1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
		Me.panel1.AutoScrollPosition = m_ptAutoScroll
	End Sub

End Class	
	
C#:
public partial class Form1 : Form 
{ 

    public Form1() 
    { 
        InitializeComponent(); 
		this.panel1.AutoScroll = true;
    } 

    private System.Drawing.Point m_ptAutoScroll = new Point(0, 0); 

    private void Form1_KeyUp(object sender, KeyEventArgs e) 
    { 
        try 
        { 
            if (e.KeyCode == Keys.Up) 
                m_ptAutoScroll = new Point(m_ptAutoScroll.X, m_ptAutoScroll.Y - 1); 
            else if (e.KeyCode == Keys.Down) 
                m_ptAutoScroll = new Point(m_ptAutoScroll.X, m_ptAutoScroll.Y + 1); 
            else if (e.KeyCode == Keys.Left) 
                m_ptAutoScroll = new Point(m_ptAutoScroll.X - 1, m_ptAutoScroll.Y); 
            else if (e.KeyCode == Keys.Right) 
                m_ptAutoScroll = new Point(m_ptAutoScroll.X + 1, m_ptAutoScroll.Y); 
            else if (e.KeyCode == Keys.PageDown) 
                m_ptAutoScroll = new Point(m_ptAutoScroll.X, m_ptAutoScroll.Y + 10); 
            else if (e.KeyCode == Keys.PageUp) 
                m_ptAutoScroll = new Point(m_ptAutoScroll.X, m_ptAutoScroll.Y - 10); 
            else if (e.KeyCode == Keys.Home) 
                m_ptAutoScroll = new Point(m_ptAutoScroll.X - 10, m_ptAutoScroll.Y); 
            else if (e.KeyCode == Keys.End) 
                m_ptAutoScroll = new Point(m_ptAutoScroll.X + 10, m_ptAutoScroll.Y); 
            this.panel1.Invalidate(); 
        } 
        catch { } 
    } 

    private void panel1_Paint(object sender, PaintEventArgs e) 
    { 
        this.panel1.AutoScrollPosition = m_ptAutoScroll; 
    } 

} 
	

Of course this is to be taken purely as an example - it doesn't account for conflicts with other controls on the form that might handle the same keystrokes, and doesn't support holding down any of the specific keys for a repeated stroke.



Questions or Comments? .

VB to C# and C# to VB translation provided by Instant C# and Instant VB.