Pergunta

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.

Foi útil?

Solução

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);
    }
}

Outras dicas

Graphics g = this.CreateGraphics();

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

Graphics g = groupBox.CreateGraphics();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top