me.creategraphics() on a user control

mcdonger

Member
Joined
Mar 18, 2007
Messages
16
Location
England
Programming Experience
1-3
I have a user control class, on which I am creating a graphics object and drawing.

I am creating the graphics object, g, as a global variable:

VB.NET:
Dim g As Graphics = Me.CreateGraphics

when I draw using this graphics object, for example:

VB.NET:
g.FillEllipse(mybrush, New RectangleF(2, 2, 240, 240))

It crops it at 150x150 pixels. My assumption is that using "me.creategraphics()" means that the graphics object is only of size 150x150, despite the fact that the usercontrol is larger.
I noticed that 150x150 is the default size when you add a usercontrol, although changing the size in design view or Windows Form Designer code makes no difference, it always crops there.

If I create the graphics object from e.graphics in the overriden paint event:

VB.NET:
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim g As Graphics = e.Graphics
End Sub

then no crop occurs. However I need to access the graphics object from other procedures, for setting properties and the like.

Thanks in advance for any help :)
 
However I need to access the graphics object from other procedures, for setting properties and the like.
Do painting in Paint event/OnPaint. To make Paint happen from elsewhere call Invalidate/Update or Refresh. The usercontrol is your class and you can write in any private fields values accessible for painting.
 
Back
Top