I'm using a ScrollableControl
in my C# project and I'd like to know how to map the arrow keys to vertical/horizontal scrolling.
EDIT : my picture box gets the focus, and i managed to map the keys to scroll. The issue here is that when i hit an arrow key, it scrolls one time and then loses the focus to give it to a button next to the Scrollviewer. I'd like the scrollviewer NOT to lose that focus
This is my code, the second paragraph is the interesting one here.
private void drawMap(IGame game)
{
System.Windows.Forms.PictureBox pictureBox = new System.Windows.Forms.PictureBox();
pictureBox.Width = (int)Math.Sqrt((double)game.Map.grid.Count) * 50; pictureBox.Height = (int)Math.Sqrt((double)game.Map.grid.Count) * 50;
pictureBox.Paint += new System.Windows.Forms.PaintEventHandler(game.Map.afficher);
pictureBox.MouseEnter += pictureBoxFocus;
pictureBox.Click += pictureBoxFocus;
pictureBox.Click += mapClicked;
System.Windows.Forms.ScrollableControl sc = new System.Windows.Forms.ScrollableControl();
sc.Controls.Add(pictureBox);
sc.AutoScroll = true;
windowsFormsHost1.Child = sc;
}
Thank you in advance :)
PLUS : my scrolling algo is kinda dirty and don't works very well so i was wondering if there was an "easy way" to do it.
private void sc_PreviewKeyDown(object sender, System.Windows.Forms.PreviewKeyDownEventArgs e)
{
System.Windows.Forms.ScrollableControl sc = (System.Windows.Forms.ScrollableControl)sender;
switch (e.KeyValue)
{
case (int)System.Windows.Forms.Keys.Down:
sc.VerticalScroll.Value += 50;
break;
case (int)System.Windows.Forms.Keys.Up:
if(sc.VerticalScroll.Value - 50 < 0)
sc.VerticalScroll.Value = 0;
else sc.VerticalScroll.Value -= 50;
break;
case (int)System.Windows.Forms.Keys.Right:
sc.HorizontalScroll.Value += 50;
break;
case (int)System.Windows.Forms.Keys.Left:
if (sc.HorizontalScroll.Value - 50 < 0)
sc.HorizontalScroll.Value = 0;
else sc.HorizontalScroll.Value -= 50;
break;
}
windowsFormsHost1.Child.Focus();
}