Question

Its a simple and straightforward question. I couldn't draw a rectangle inside a groupbox. Any answer will be really helpful.

    private void groupBox_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = this.CreateGraphics();
        Brush b = new SolidBrush(Color.Gray);
        g.FillRectangle(b, 800, 70, 360, 440);
    }

I think there was sth wrong with my ide. I had tried Graphics g = e.Graphics before. I tried again after restarting my computer. Now its working. I think something happened to my computer or visual studio. Thanks for your support.

Was it helpful?

Solution

Instead of this.CreateGraphics() you have to use e.Graphics

Example:

private void groupBox_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    using (Brush b = new SolidBrush(Color.Gray))
    {
        g.FillRectangle(b, 800, 70, 360, 440);
    }
}

OTHER TIPS

Graphics g = this.CreateGraphics();

Instead of this statement use your groupBoxName you want to draw on, like this

Graphics g = groupBox.CreateGraphics();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top