Reputation: 333
Can you explain me what's wrong with this code? because it is not drawing anything. doesn't it suppose to draw a rectangle in my form? thanks!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Graphics g = this.CreateGraphics();
Rectangle r = new Rectangle(0, 0, 150, 150);
g.DrawRectangle(System.Drawing.Pens.Black, r);
}
}
Upvotes: 1
Views: 1027
Reputation: 8868
Make your painting in the OnPaint
method:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Rectangle r = new Rectangle(0, 0, 150, 150);
g.DrawRectangle(System.Drawing.Pens.Black, r);
}
Upvotes: 2