Drawing within Winforms Paint() event doesn't work

Within override func OnPaint() code such as that below works as expected.

var pt       = Point(50,50),
    newPoint = Point.Empty

newPoint.X=100
newPoint.Y=200

var g  = Graphics.FromHwnd(self.Handle),
    pn = Pen(Color.Blue, 4)

g.DrawLine(pn, pt, newPoint)
pn.Dispose()
g.Dispose()

However, if placed within private func MainForm_Paint() the expected line is not showing on the form. The analogous code when done in (Microsoft, not RemObjects) C# and placed in Form1_Paint(), works.

Can I see an concrete example (both Silver, and working VC#)? Where/who calls private func MainForm_Paint()?

Visual C# version:

Corresponding code in Silver (that doesn’t work/won’t show anything on the form):

Just to be clear, you do have the “Paint” event on the form set to MainForm_Paint right?

If so, can you send the silver version?

You mean this?

So, the Silver version is just any simple GDI+ Graphics object drawing code:

private func MainForm_Paint(_ sender: System.Object!, _ e: System.Windows.Forms.PaintEventArgs!) {
    var pt       = Point(50,50),
        newPoint = Point.Empty

    newPoint.X=100
    newPoint.Y=200

    var g  = Graphics.FromHwnd(self.Handle),
        pn = Pen(Color.Blue, 4)

    g.DrawLine(pn, pt, newPoint)
    pn.Dispose()
    g.Dispose()
}

within the Paint event for the Main form and this way of doing it fails in Silver but works in Visual C#. I haven’t tried in RemObjects C# yet.

Hrmm; when I try the code above:

I get that. What am I missing?

Hmm… ok, so I try the same again and it works and then I move code around a little, but after a while, it stops working…

Here’s a zip of the project…

SilverWinformsGDIPlus1.zip (280.4 KB)

I think if you override override func OnPaint(_ e: PaintEventArgs!) { it won’t call the pain event handler.

Yup. That was it. My bad, sorry.

1 Like