I tried to create a code in c # to create a rectangle with the mouse but it has not worked for me.
Can someone help me?
public partial class Form1 : Form
{
public Form1()
{
}
private void Form1_Load(object sender, EventArgs e)
{ }
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
using (Graphics g = this.panel1.CreateGraphics())
{
Pen pen = new Pen(Color.Black, 2);
Brush brush = new SolidBrush(this.panel1.BackColor);
g.FillRectangle(brush, this.panel1.Bounds);
g.DrawRectangle(pen, e.X, e.Y, 20, 20);
pen.Dispose();
brush.Dispose();
}
}
private Panel panel1;
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
this.panel1.Cursor = System.Windows.Forms.Cursors.Cross;
this.panel1.Location = new System.Drawing.Point(-2, -2);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(676, 429);
this.panel1.TabIndex = 0;
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
this.panel1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
this.ClientSize = new System.Drawing.Size(674, 424);
this.Controls.Add(this.panel1);
this.Cursor = System.Windows.Forms.Cursors.Cross;
this.Name = "Form1";
this.Text = "rectangles";
this.Load += new System.EventHandler(this.Form1_Load_1);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
this.ResumeLayout(false);
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
}
}