Resolved Using OnPaint method of an other class

PwUP

Member
Joined
Sep 21, 2009
Messages
23
Programming Experience
1-3
Hi, i'm a new comer here.
First of all i'm not english, so forgive me whether i'll write something wrong.;)
(nice smilies!)
I'm trying to use the OnPaint method of the class "Form1" from an other class, my purpose is to draw something in form1 like a rectangle or a string.
What should i use?:confused:
Thanks in advance
 
Last edited:
Use the Paint event.
 
Hi, thank you for the reply.
I tried defining a variable in form1 with this code:

public mygraphics as graphics = me.creategraphics

Then, in paint event of class1 i put this:

dim newimg as image = image.fromfile("this file exists")
form1.mygraphics.drawimage(newimg,0,0)

This seem to function properly, but there are a few bugs i want to fix.
First, thers a bad looking flicker effect, i used the doublebuffer property on form1 but nothing changes.
Second, there is a "limited area" for drawing in form1 which i can't draw out.
It's like a square at 0,0 point where width and height equals to 100.
For example, a line with points 0,0 and 300,300 is shown as 0;0 and 100;100
and the image drawed appears cropped.
:mad::eek:
Help me again please :cool:
 
Same as the OnPaint you use the Graphics instance provided by the handler method to draw with, eg e.Graphics.

To attach to the Paint event you can use AddHandler statement, or just declare a WithEvents variable pointing to the relevant object.
 
Here's a code sample using a WithEvents variable, this is convenient since there is a ParentChanged event that can tell when the control is attached to new parents, since the control can be parented to any other control the sample targets the ParentForm property for these changes. Using a WithEvents variable also has the benefit that you don't have to release the old event handler with RemoveHandler when parent changes.
VB.NET:
Private WithEvents ParentFormRef As Form

Private Sub UserControl1_ParentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ParentChanged
    ParentFormRef = Me.ParentForm
End Sub

Private Sub ParentFormRef_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles ParentFormRef.Paint
    e.Graphics.FillRectangle(Brushes.Aquamarine, Me.ParentForm.ClientRectangle)
End Sub
I have to add that it's rather unusual for a control to do painting outside it's own designated window like this, but as you can see it's possible.
 
Back
Top