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?????

Recommended Answers

All 4 Replies

Not sure what you are asking. It is using the mouse click (down/up/move) now. Do you only want it to work when you click a button first?

You can draw directly to the form using a Graphic object from the CreateGraphics method. e.g.

var g = this.CreateGraphics();
g.DrawRectangle(t,l,x,y);

Note that anything draw in this way is temporary as it will be erased on the next refresh.

@Momerath: I think the OP wants to show the rectangle while the mouse is moving so the user can see what they are drawing.

Not sure what you are asking. It is using the mouse click (down/up/move) now. Do you only want it to work when you click a button first?

i want it in button...as in paint when we select rectangle then only it allows us to draw rectangle similarly i am making paint in c# when i click on button rectangle then only it should allow to do so...but in above code when we run application it draws on form....i implemented the above code in button(move,up,down,paint) but it does not do anything....

You need to do the draw conditionally.
I will assume that the button is a CheckBox or Radiobutton as these stay down when clicked and have a Checked property.
When you click the button the Checked propery is true so use this to "Enable" the rectangle draw.
E.g.

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
	if (e.Button == MouseButtons.Left)
	{
		if (RectangleButton.Checked)
		{
			rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
			this.Invalidate();
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.