TtT23
TtT23

Reputation: 7030

OnPaint method not drawing the rectangle properly

I have the following paint event (form) that is drawing the rectangle:

void LogicSimulationViewerForm_Paint(object sender, PaintEventArgs e) {
    Rectangle rect = new Rectangle(100, 100, 400, 100);
    Graphics c = rtbLogicCode.CreateGraphics();
    c.DrawRectangle(new Pen(Color.Black, 3), rect);
}

The rectangle is shown for a brief moment and then disappears immediately. The rectangle will only show momentarily again when the user resizes the form.

How can I solve this issue?

Upvotes: 0

Views: 1355

Answers (1)

DmitryG
DmitryG

Reputation: 17850

Don't use the Control.CreateGraphics() method, use the PaintEventArgs.Graphics property:

void LogicSimulationViewerForm_Paint(object sender, PaintEventArgs e) {
    Rectangle rect = new Rectangle(100, 100, 400, 100);
    e.Graphics.DrawRectangle(Pens.Black, rect);
}

Upvotes: 6

Related Questions