简体   繁体   English

如何使用按键上下移动面板

[英]How to move up/down a Panel using keys

I've a picturebox on a form window. 我在窗体窗口上有一个图片框。 In this picturebox I want to move the panel (as a circle panel) up and down using keys only if I click on it. 在此图片框中,仅当我单击面板时,才想使用键上下移动面板(作为圆形面板)。

IE: there are 3 panels having the following X,Y: IE:有3个面板,其中以下X,Y:

  • panel1(5,5) panel2(10,5) panel3 (15,5) 面板1(5,5)面板2(10,5)面板3(15,5)

So if I click on panel1 I get focus on it and only it and then if I click on upKey or DownKey I move this panel up or down in the picturebox. 因此,如果我单击panel1,则将注意力集中在它上面,只有它,然后单击upKey或DownKey,即可在图片框中向上或向下移动此面板。 Clicking on another object you leave the focus on the panel. 单击另一个对象,将焦点放在面板上。

Same thing if I click on panel2 or panel3. 如果我单击panel2或panel3,也是一样。

Any help and tip is appreciated. 任何帮助和提示,不胜感激。

Thanks 谢谢

UPDATE #2 更新#2

I did some changes and now I can move up or down a panel by clicking on it. 我做了一些更改,现在我可以通过单击面板来上下移动面板。 The problem now is that if I click on panel1 to get focus and move it up or down, it moves with panel2 togheter. 现在的问题是,如果我单击panel1来获得焦点并将其向上或向下移动,则它会与panel2弯曲移动。 I want to move just panel1 if i click on it.. Setting up a new bool property of the panel object that enable or disable it on click it seems that doesn't work and by clicking on panel and then on up/down keys nothing happens. 我只想移动panel1,如果我单击它。设置面板对象的新bool属性,在单击时启用或禁用它,这似乎不起作用,并且通过单击panel然后按向上/向下键不起作用发生。

Following there is the full code of my two updated classes: 以下是我的两个更新的类的完整代码:

Class CircleButton (panel) 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();
    }

}

Form class 表格类

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);
    }



}

How can I fix this? 我怎样才能解决这个问题?

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();
            }
        }     
    }

The problem you're having is that your if statements are not in a KeyDown event . 您遇到的问题是if statements不在KeyDown event They way you're using the e.KeyCode is correct. 他们使用e.KeyCode是正确的。 If you need any more information on KeyDown and/or KeyPress event i suggest clicking this link 如果您需要有关KeyDown和/或KeyPress事件的更多信息,建议您单击此链接

I hope this helps if you have any further questions feel free to ask. 如果您有任何其他疑问,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM