Edited version:
Without much assumpition of what you trying to do:
private void panel1_Click(object sender, EventArgs e) {
using (Graphics g = this.panel1.CreateGraphics()) {
Pen pen = new Pen(Color.Black, 2);
Brush brush = new SolidBrush(this.panel1.BackgroundColor);
g.DrawRectangle(pen, 100,100, 100, 200);
pen.Dispose();
}
}
Your code did not work as it is drawing the rectangle on the window (this) and the drawn rectangle is then hidden by your panel.
Generally overriding Paint for such a simple case is just too much effort for just drawing a rectangle on a panel. However, drawing the rectangle in this way works, but the rectangle will disapear when the form is redrawn (e.g. by minimizing and subsequently showing the form again. If the rectangle has to be persistent you will have to use the paint method and for this you will have to (e.g.) create the rectangle in the click event and then draw it in the paint event. (See roygbiv's solution for such an approach). Note: If you go along with the paint method, you should keep it as efficient as possible, as the paint method gets called verry frequently.
Edit 2
You do not need to clear the background as your rectangle will be drawn always at the same place. In order to draw the rectangle at the point where the user cliced (it is an assumption that this is what you want) you should move the code to the mouse down event, e.g.:
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); // redraws background
g.DrawRectangle(pen, e.X, e.Y, 20, 20);
pen.Dispose();
brush.Dispose();
}
}