There are a number of issues with your snippet, but its just a snipet, so I won't go into that....
Here is a snippet I wrote for you, starting with your own code.
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DW_Rect1
{
public partial class Form1 : Form
{
private bool _start = false;
private Rectangle _rect = new Rectangle(10, 10, 0, 0);
public Form1()
{
InitializeComponent();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_start = true;
_rect.X = e.Location.X;
_rect.Y = e.Location.Y;
_rect.Width = 0;
_rect.Height = 0;
}
else
_start = false;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_start && e.Button == MouseButtons.Left)
{
_rect.Width = e.X - _rect.X;
_rect.Height = e.Y - _rect.Y;
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Red, _rect);
}
}
}
Review the changes, and ask questions about why I did this or that.
// Jerry