Basically you need to check that you are not out of bounds of the picture box.
1. Add a
Panel
called
panel1
to the Form.
2. Set the property
form1.PreviewKey = true
3. Set the property
panel1.AutoScroll = true
4. Set the property
panel1.TabStop = true
(in order to be able to set focus to the control)
5. Add the
PictureBox
inside the panel and call it
pictureBox1
6. Set the property
pictureBox1.SizeMode = AutoSize
Then add the code like this.
The step size is of course up to your own desire.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
if ((panel1.HorizontalScroll.Value + panel1.HorizontalScroll.SmallChange) >= panel1.HorizontalScroll.Maximum)
panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Maximum;
else
panel1.HorizontalScroll.Value += panel1.HorizontalScroll.SmallChange;
}
else if (e.KeyCode == Keys.Left)
{
if ((panel1.HorizontalScroll.Value - panel1.HorizontalScroll.SmallChange) < panel1.HorizontalScroll.Minimum)
panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Minimum;
else
panel1.HorizontalScroll.Value -= panel1.HorizontalScroll.SmallChange;
}
}
Maybe this CodeProject article can help you further.
Fast Image Scrolling in C#[
^]