What is right way to show large image in Winforms app with scroll-bars and keyboard scroll support? Currenty i'm use Panel(AutoScroll=True) with nested PitureBox (SizeMod = AutoSize).
I have 2 question:
1) What control select for drawing image? Panel and PitureBox cant be selected (focused) using Tab key. Using Button with AutoSize = true and FlatStyle = Flat is right solution?
2)How to scroll image in panel using keyboard. What keyboard events a need handle - Form, Panel or PictureBox. May be I should set for Panel AutoScroll=false and add to them HScroll and VScroll, that events I should handle?
What is right way for implement this elementary app?
(Just for info, main form have other Panel(Dock=Top) that contain some controls.)
As for the first question: There is no Control which is really well suited to draw on and still can get the
Focus
. In the link below you can see how to make a selectable panel, though.Now for the real problem: How to scroll an
AutoScroll Panel
by keyboard..?This is amazingly hard to do. Here is an example for a start:
I test for the
Panel
to contain the mouse. You may want to play around with the scroll amount.. Also replace theKeyValues
with properKeys
;-) SetKeyPreview = true;
for the Form!Note: This only works if there is no Control on the Form which can get the Focus.
Since most likely you have some such
Controls
, likeButtons
,ListBoxes
etc.. here is another solution, which works in any case, I believe..:This doesn't need the Form to have
KeyPreview = true;
.Here is the MSDN explanation for
AutoScrollPosition
.Here is a post that suggests using a subclass for
Panel
and/orPictureBox
, which is supposed to allow them to getFocus
. I couldn't get that to work though..(Nor does it seem to be any simpler in the end..)