Question

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);
    }
}
Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top