4

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();
}
Adrien Brunelat
  • 4,492
  • 4
  • 29
  • 42
  • What have you tried? Are you having trouble with catching the arrow keys press or with invoking a scroll? – Rotem Jan 06 '13 at 12:24
  • You do not have any controls that can receive the focus. Accordingly, you'll never get them to respond to a keystroke. You'll need to fix that by creating a smarter control that *can* receive the focus and respond to keystrokes. – Hans Passant Jan 06 '13 at 13:30
  • possible duplicate of [Panel not getting focus](http://stackoverflow.com/questions/3562235/panel-not-getting-focus) – Hans Passant Jan 06 '13 at 13:31
  • Hm 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 – Adrien Brunelat Jan 06 '13 at 14:55
  • 1
    Yes, the cursor key moves the focus to the next control. You forgot to set e.IsInputKey to true to prevent this from happening. – Hans Passant Jan 06 '13 at 15:17
  • Perfect, I just need to find how to update the scrolling bars now :) Thank you very much ! – Adrien Brunelat Jan 06 '13 at 20:36
  • This acts kind of strangely for me; the scrollbars jump around all the time. Shouldn't scrollbars react to a change of the main control, and not the other way around? – Nyerguds Nov 19 '16 at 02:09
  • Ah. Adding `sc.PerformLayout();` fixes it :) – Nyerguds Nov 19 '16 at 02:30

0 Answers0