Create panel with center at user click point

3

I need to create a Panel in a Form Getting more or less like this:

When you click the mouse on Form create Panel taking the location of the click and create the Panel centralized.

I did so:

Panel panel = new Panel();

private void criarLabel()
{
      panel.Size = new System.Drawing.Size(200, 100);
      panel.Visible = true;
      this.grid.Controls.Add(panel);
}
private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
{
     criarLabel();
}
    
asked by anonymous 25.11.2015 / 18:13

1 answer

3

Place this block of code in the event MouseClick of Form (or the control that will have Panel added).

var panel = new Panel
{
    BackColor = Color.Black,
    Height = 100,
    Width = 200
};

panel.Location = new Point(e.X - (panel.Width / 2), e.Y - (panel.Height / 2));

this.Controls.Add(panel);
    
25.11.2015 / 18:35