mardi 10 mars 2015

How to move panels up/down on keysDown event in c#

I've 4 panels, having same Y and different X, that are created at the program start on a picturebox. When I click on a panel, it sets the focus and is ready to get a keysDown event so i.e. if I click on up arrow key the panel moves up. At the moment I've two problems:



  1. panels don't redraw when Y changes

  2. clicking on different panels produce a loop of actions: i.e. if I click 1 time on panel4, then 1 time on panel1 and then again 1 time on panel4 when I try to move it up or down it does the action more than 1 time so instead of move by 10 it really moves by 30..


This is the code:



public partial class FormView : Form
{
List<CircleButton> myPanels = new List<CircleButton>(); // array of panels CircleButton
Point[] arrayPoints_milestones; // array of X,Y
int index;

public FormView()
{
InitializeComponent();

arrayPoints_milestones = new Point[4];
for (int i = 0; i < 4; i++ )
{
arrayPoints_milestones[i] = new Point { X = 20, Y = 20 };
}

test();
}

protected void panel_Click(object sender, EventArgs e)
{
CircleButton panel = sender as CircleButton;

index = (int)panel.Tag;

myPanels[index].Focus(); //panel.Focus();
myPanels[index].PreviewKeyDown += new PreviewKeyDownEventHandler(panel_KeyDown);
}

private void panel_KeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
arrayPoints_milestones[index].Y -= 10;
//debug
MessageBox.Show("" + arrayPoints_milestones[index].Y);

Invalidate();
}
if (e.KeyCode == Keys.Down)
{
arrayPoints_milestones[index].Y += 10;
//debug
MessageBox.Show("" + arrayPoints_milestones[index].Y);

Invalidate();
}
}

private void test()
{
//for (int i = 0; i < 4; i++)
int i=0;
foreach(var value in arrayPoints_milestones)
{
CircleButton panel = new CircleButton();
panel.Name = "panel" + i;
panel.Tag = i;
panel.Centre = new Point(arrayPoints_milestones[i].X + i * 10, arrayPoints_milestones[i].Y);
panel.Radius = 10;
panel.BackColor = Color.Red;
panel.Message = "Index: " + panel.Tag.ToString();

myPanels.Add(panel);

pictureBox1.Controls.Add(myPanels[i]);
myPanels[i].Click += new EventHandler(panel_Click);

i++;
}
}
}


and this is the custom panel class:



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

public string Name
{
get;
set;
}

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

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

public string Message { get; set; }

public CircleButton()
{
//Default Values
Name = "panel_base";
this.BackColor = Color.Black;
Radius = 1;
Centre = new Point(0, 0);
this.DoubleBuffered = true;
}

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

Aucun commentaire:

Enregistrer un commentaire