如何使用键向上/向下移动面板

时间:2015-03-06 10:07:37

标签: c# winforms events keypress

我在表单窗口上有一个图片框。在这个图片框中,我只想在点击它时使用按键上下移动面板(作为圆形面板)。

IE:有3个面板具有以下X,Y:

  • panel1(5,5)panel2(10,5)panel3(15,5)

因此,如果我点击panel1,我会专注于它,只有它,然后如果我点击upKey或DownKey,我会在图片框中向上或向下移动此面板。单击另一个对象,将焦点保留在面板上。

如果我点击panel2或panel3,也是一样。

感谢任何帮助和提示。

感谢

更新#2

我做了一些更改,现在我可以点击它来向上或向下移动面板。 现在的问题是,如果我点击panel1来获得焦点并向上或向下移动它,它会随着panel2 togheter移动。如果我点击它,我想移动panel1 .. 设置面板对象的新bool属性,在单击它时启用或禁用它似乎不起作用,单击面板然后上/下键没有任何反应。

以下是我的两个更新类的完整代码:

Class CircleButton(面板)

public class CircleButton : Panel
{
    //Properties to draw circle
    float radius;
    public float Radius
    {
        get { return radius; }
        set
        {
            radius = value;
            this.Size = new Size((int)Radius, (int)Radius);
        }
    }


    Point centre;
    public Point Centre
    {
        get { return centre; }

        set
        {
            centre = value;
            this.Location = Centre;
        }
    }

    public string Message { get; set; }

    bool active;
    public bool Active
    {
        get { return active; }

        set
        {
            active = value;
            this.Enabled = active;
        }
    }

    public CircleButton()
    {
        //Default Values
        this.BackColor = Color.Black;
        Radius = 1;
        Centre = new Point(0, 0);
        Active = false;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        //Defines a graphic path and set it as the panel's region
        //For custom region use different path's
        if (centre != null)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(0, 0, radius, radius);
            this.Region = new Region(path);
            path.Dispose();
        }
    }


    protected override void OnKeyDown(KeyEventArgs e)
    {
        base.OnKeyDown(e);
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);

        Focus();
    }

}

表单类

public partial class FormView : Form
{      
    private int _x;
    private int _y;
    CircleButton panel;

    public FormView()
    {
        InitializeComponent();

        _x = 20 ;
        _y = 20 ;            
    }

    protected void panel_Click(object sender, EventArgs e)
    {
        //Display Message
        //panel = (CircleButton)sender;
        //MessageBox.Show(panel.Message);          
        panel.Focus();
        panel.Active = true;

        panel.PreviewKeyDown += new PreviewKeyDownEventHandler(panel_KeyDown);
    }

    private void panel_KeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (panel.Active == true)
        {
            if (e.KeyCode == Keys.Up)
            {
                _y -= 10;
                Invalidate();
            }
            if (e.KeyCode == Keys.Down)
            {
                _y += 10;
                Invalidate();
            }
        }
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        panel = new CircleButton();
        panel.Centre = new Point(_x, _y);
        panel.Radius = 10;
        panel.BackColor = Color.Red;
        panel.Active = false;

        pictureBox1.Controls.Add(panel);
        panel.Click += new EventHandler(panel_Click) ;

        panel = new CircleButton();
        panel.Centre = new Point(_x+50, _y);
        panel.Radius = 10;
        panel.BackColor = Color.Black;
        panel.Active = false;

        pictureBox1.Controls.Add(panel);
        panel.Click += new EventHandler(panel_Click);
    }



}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

private void Form1_KeyDown(object sender, KeyEventArgs e)   
    {
        if (isMoving == true)
        {
            if (e.KeyCode == Keys.Up)
            {
                _y -= 10;
                Invalidate();
            }
            if (e.KeyCode == Keys.Down)
            {
                _x -= 10;
                Invalidate();
            }
        }     
    }

您遇到的问题是if statements不在KeyDown event。他们使用e.KeyCode的方式是正确的。如果您需要有关KeyDown和/或KeyPress事件的更多信息,建议您点击this link

如果您有任何其他问题可以随时提出,我希望这会有所帮助。