i am making paint in c#.when i am drawing a rectangle by the below code then it is working:
namespace deniweb2
{
public partial class Form1 : Form
{
Rectangle rect;
List<Rectangle> therectangles = new List<Rectangle>();
public Form1()
{
InitializeComponent();
this.Cursor = System.Windows.Forms.Cursors.Cross;
this.DoubleBuffered = true;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
rect = new Rectangle(e.X, e.Y, 0, 0);
this.Invalidate();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Left )
{
rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
}
this.Invalidate();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (rect!=null && therectangles .Contains(rect)==false )
{
therectangles.Add(rect);
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Red, 2))
foreach (Rectangle r in therectangles)
e.Graphics.DrawRectangle(pen, r);
}
}
}
i want to use this code in click event of button.how to do so?????