Forms Drawing Problem

sapator

Member
Joined
Oct 4, 2006
Messages
23
Programming Experience
1-3
Hi.
I have a problem with drawing on forms.
I use a form (with showdialog) to add forms in a main form that is shown behind the showdialog form.
The problem is that when i use the graphics.drawline to draw lines between the forms, it only works AFTER i close the showdialog form.
(meaning that if i use the function again when i close the dialogform, then it works)
.
I see that it works when i use the function to draw the forms inside the main forms Paint event.
I don't wanna do that cuz i use other functions to draw, when a form is moved or resized. Think of it like the Relationship lines in an Office-Access Application.
Bottom line is, how can i make a line show at the main form when i call it from my showdialog form, WITHOUT having to use mainforms Paintevent.
P.S.
I use painteventargs in my functions in order to draw the lines like this:

VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] DrawLinePoint([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] PaintEventArgs, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] pMain [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Point, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] pSec [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Point)
[/SIZE][SIZE=2][SIZE=2]e.Graphics.DrawLine([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Pen(Color.Black), pMain, pSec)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[/SIZE]
it works(when the showdialog form is active) only when i put the function to run from the mainforms Paint event and not when i call it individually.
 
It's not going to work because all painting should be done with the graphics object supplied by the paint event, even if the actual code for the painting doesn't reside in that sub routine. You can simply pass it to your subfrom the paint event. Failing that the only other wat would be to create a graphics object from the hWnd or the Dc

VB.NET:
Dim G as graphics = Graphics.FromhWnd(YourForm.Handle)
 
Or
 
Dim G as graphics - graphics.fromhDc(Inptr that points to your forms device context)


Personally i'd stick to the paint event, why wouldn't you want to use it if it works. You can always call me.Invalidate to force a repaint of the form whenever necessary.
 
Im not realy sure that the paint event will use the function that i call or all the functions that reside inside it. I don't just have 1 function for painting.
l will try your code and then i'll try to put all the functions in the paint event. I have tried to put a boolean to control the functions but since the paint event is using itself constantly i'm not sure how to control it.
 
i'm trying the
VB.NET:
 Dim G as graphics = Graphics.FromhWnd(YourForm.Handle)
it does not work.
YourForm is the shodialogform or the mainform.
I'm assuming the showdialog form.
But it does not work , or i'm using it wrong.
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Grr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics = Graphics.FromHwnd(Showdialogform[/SIZE][SIZE=2].Handle)
[/SIZE][SIZE=2][COLOR=#008000]' Dim grr As Graphics
[/COLOR][/SIZE][SIZE=2]' i also used this... Grr = showdialogform[/SIZE][SIZE=2].CreateGraphics
[/SIZE]
 
VB.NET:
Private PaintALine as Boolean
 
Protected Overiddes Paint(byval Sender as object, byval e as PaintEventArgs)
 
Mybase.OnPaint(e)
 
If Me.PaintALine Then
Me.DrawMyLine(e.graphics)
End If
 
End Sub
 
 
 
Private Sub DrawMyLine(byval e as graphics)
 
e.drawline(Pens.Black,50,50,100,50)
End Sub
 
Private Sub MakeLineAppear
Me.PaintALine = true
Me.Invalidate
End Sub
 
Private MakeLineDisappear
Me.PaintALine = False
Me.Invalidate
End Sub

The above example is a very simple way of controling the paint event with conditional statement. If you call the MakeLineAppear sub from a button click or somthing then a line will appear on the for at the co-ordinates i have specified. If you then call the MakeLineDisappear Sub the line will er.. disappear. This is because you are changing the boolean varaible that is the condition that is tested for inside the paint event then calling Me.Invalidate forces a repaint of the form.
 
Hi.
Your example does not work.
(i guess you wanted to write overrides onpaint etc, i've corrected but it does not work)
I've also used "New Pen" but nothing...
 
What bit about it doesn't work? You may need to add the following in the forms contructor...

VB.NET:
Mybase.SetStyle(ControlStyles.AllPaintingInWM_Paint,True)
Mybase.SetStyle(ControlStyles.DoubleBuffer, True)
Mybase.SetStyle(ContrlStyles.UserPaint,True)

There's is nothing wrong with my code and it should work just fine. Try adding the above lines in the forms contructor and if it still doesn't work then post the project and i'll tell you why. However as i have said, there is no reason why it should not work if you have placed the code inside the overriden paint event.
 
I've written the exact code on a new application and added the extra code you gave me below the forms designer "after Me.ResumeLayout(False)"
I press the button but it does not do anything.
Also the problem i have is that even if it draws a line, when i want to add another line in the form it will erase the line that is drawn. I don't want this to happen.

 
It won't erase that line if you don't change that boolean to false, or you can just create a array of Point structures and pass it to the drawlines method. Post the project and i'll take a look.
 
Also the problem i have is that even if it draws a line, when i want to add another line in the form it will erase the line that is drawn. I don't want this to happen
Drawing with Paint event isn't persistant, each time Paint happens it will repaint the full surface by the drawing specifications given. If you need to persist drawing you must either (A) keep collection of drawing objects that will be drawn each time Paint event occurs or (B) draw to an bitmap and display that image. You can combine A and B for a combination of persistant and non-persistant graphics.
 
As i said, if you create an array of Point Structures as a class level variable you can add to those points at will from anywhere. Then when you call invalidate the drawlines method will draw a line through all the points in the array. You don't necessarily need a collection of drawing objects here, or to go as far a bitmap.
 
Hi.
Listen i solved the problem BUT LISTEN TO THIS!
1st i had my form show up before i draw. Let's say that this is somewhat logical.
2nd if you have breakpoints and some of them hit then the lines will not draw!!!!!!!!!!!!!!!!!!!!! Unbelievable! Microsoft did it again!! (i have 2005 team edition).
Now about the paint object. I don't use it. I use graphics directly. The problem is that you have frigtened me with what u said about lines that will be removed. Does this aplly also if you don't use the paint object (thus painteventargs) or i'm cool now?
Thanks.
 
If you use CreateGraphics for drawing to a control you're far worse in trouble, because you will have lost contact with the paint events totally and when this happens it repaints fresh (empty) over your graphics without you knowing (and this could happen many times a second). Work with the flow instead of agains it.

Breakpoints during live painting doesn't work very well for obvious reasons.
 
As i said, if you create an array of Point Structures as a class level variable you can add to those points at will from anywhere. Then when you call invalidate the drawlines method will draw a line through all the points in the array. You don't necessarily need a collection of drawing objects here, or to go as far a bitmap.
An array of Point Structures or similar will serve fine for my general term "collection of drawing objects". It's anyway just the methology for dynamically painting what you request each time the paint event happen.
 
Can you give me an example?
Let's say that i have the line coordinates between my forms in an arraylist or a dataset.
Ok the paint event hits multiple times. What must i do? Have in mind that i want to paint and remove the lines when the forms move (locationchange and sizechange events).
Also what am thinking is that everytime i draw a line and let's say have a function that is called inside paint, the function will call some coordinates to draw. Ok what will happen to the previous coordinates i had? Will they stay? Will they be erased?
 
Back
Top