Drawing within Winforms Paint() event doesn't work


(John Perez) #1

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.


(marc hoffman) #2

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


(John Perez) #3

Visual C# version:

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


(Carlo Kok) #4

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?


(John Perez) #5

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.


(Carlo Kok) #6

Hrmm; when I try the code above:

I get that. What am I missing?


(John Perez) #7

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)


(Carlo Kok) #8

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


(John Perez) #9

Yup. That was it. My bad, sorry.