개발/C#

C# 패널 스크롤바 키보드로 움직이기

FA1976 2019. 7. 9. 08:28

Form 안에 패널이 있고 이 패널 안에 Picture 박스가 있다고 치자.

이 Picture 박스의 그림이 패널 보다 크면 스크롤이 생기는데

키보드를 이용해서 내리고 싶을때가 있다. 이럴때 아래 코드를 사용하면 된다.

Form에서 Keydown 이벤트를 발생시키고 키다운 /업 에따라 처리하면 된다.

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Down)
            {
                Point current = panel_command.AutoScrollPosition;
                Point scrolled = new Point(current.X, -current.Y + 50);
                panel_command.AutoScrollPosition = scrolled;
            }
            else if(e.KeyCode == Keys.Up)
            {
                Point current = panel_command.AutoScrollPosition;
                Point scrolled = new Point(current.X, -current.Y - 50);
                panel_command.AutoScrollPosition = scrolled;
            }
            else if(e.KeyCode == Keys.Right)
            {
                Point current = panel_command.AutoScrollPosition;
                Point scrolled = new Point(current.X, -current.Y + 100);
                panel_command.AutoScrollPosition = scrolled;
            }
            else if (e.KeyCode == Keys.Left)
            {
                Point current = panel_command.AutoScrollPosition;
                Point scrolled = new Point(current.X, -current.Y - 100);
                panel_command.AutoScrollPosition = scrolled;
            }
            else if((e.KeyCode == Keys.Escape)||(e.KeyCode == Keys.S))
            {
                this.Close();
            }
        }