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.
 
sapator said:
Will they stay? Will they be erased?
I beleive JohnH answered that already:
Drawing with Paint event isn't persistant, each time Paint happens it will repaint the full surface
Bob Powell has a good set of tutorials for understanding GDI+, it can be found here: Bob Powell's Beginners Guide to GDI+
sapator said:
I use a form (with showdialog) to add forms in a main form that is shown behind the showdialog form.
I'm really not following what you're doing. Perhaps you could explain the purpose of the application.
 
I beleive JohnH answered that already:
Bob Powell has a good set of tutorials for understanding GDI+, it can be found here: Bob Powell's Beginners Guide to GDI+I'm really not following what you're doing. Perhaps you could explain the purpose of the application.

Thanks for the tutorial, i'll look into it.
Yes i can explain. Think of it as the query designer in Access. A form is shown(showdialog) and you select the table which is added in the backgroung along with other tables with lines showing the relations.
(actually i used dialogresponse and i remove the showdialog forrm now. If i want to add another table i reopen the showdialogform).
The problem i have is that i didn't want to use the paint event cuz i will use other functions when i move or resize the tables but if this is the only way i could use a little help on how to use a arraylist or dataset so that my lines would stay and also in place when tables are added or moved.
 
A line is drawn between two points, to make it easier create a Line class/structure with these two properties StartPoint and EndPoint. Use a List(Of Line) to maintain the collection of lines. In Paint you do For Each draw these lines.
VB.NET:
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Structure[/COLOR][/SIZE][SIZE=2] Line[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] StartPoint, EndPoint [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Point[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Structure[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] myLines [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] List([/SIZE][SIZE=2][COLOR=#0000ff]Of[/COLOR][/SIZE][SIZE=2] Line)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Paint([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.PaintEventArgs) _[/SIZE]
[SIZE=2][COLOR=#0000ff]Handles [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Paint[/SIZE]
[SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] l [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Line [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] myLines[/SIZE]
[SIZE=2]e.Graphics.DrawLine(Pens.Blue, l.StartPoint, l.EndPoint)[/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
Now If you add (or remove) a line to the list collection it will be drawn (or not drawn) by Paint "next time" or by your Refresh/Invalidate request.
 
I was thinking to use a dataset so i could have the name of the form and it's location and also the form that has a relattionship with it.
So i guess when i use invalidate the removed rows will not draw.
Ok i'll try and if i have problems i'll post them.
Thanks again.
 
Hi.
I'm asking for your help again because i run into some problems.
Ok first i'll post my code and then explain (exuse some Greek comments in the code :) )

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Paint([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.PaintEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Paint
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strTableName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strRefTableName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strLocationMain [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] intTable [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] x = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataSet1.Tables(0).Rows.Count - 1
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataSet1.Tables(0).Rows(x).Item(0).ToString = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataSet1.Tables(0).Rows(x).Item(2).ToString [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Continue[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]For
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' maintables main table column
[/COLOR][/SIZE][SIZE=2]strTableName = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataSet1.Tables(0).Rows(x).Item(0).ToString
[/SIZE][SIZE=2][COLOR=#008000]' maintables main table column location
[/COLOR][/SIZE][SIZE=2]strLocationMain = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataSet1.Tables(0).Rows(x).Item(1).ToString
locationMain.X = pointconvertor([/SIZE][SIZE=2][COLOR=#800000]"x"[/COLOR][/SIZE][SIZE=2], strLocationMain)
locationMain.Y = pointconvertor([/SIZE][SIZE=2][COLOR=#800000]"y"[/COLOR][/SIZE][SIZE=2], strLocationMain)
[/SIZE][SIZE=2][COLOR=#008000]' referenced maintables column
[/COLOR][/SIZE][SIZE=2]strRefTableName = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataSet1.Tables(0).Rows(x).Item(2).ToString
intTable = arrayTablesInForm1.IndexOf(strRefTableName)
[/SIZE][SIZE=2][COLOR=#008000]' Psaxno na do an iparxei mesa stin mainform to table
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' an oxi continue for. An nai tote perno to location tou
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] intTable = -1 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Continue[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]For
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strRefTableLoc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] y [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] y = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataSet1.Tables(0).Rows.Count - 1
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataSet1.Tables(0).Rows(y).Item(0).ToString = strRefTableName [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]strRefTableLoc = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataSet1.Tables(0).Rows(y).Item(1).ToString
locationSecond.X = pointconvertor([/SIZE][SIZE=2][COLOR=#800000]"x"[/COLOR][/SIZE][SIZE=2], strRefTableLoc)
locationSecond.Y = pointconvertor([/SIZE][SIZE=2][COLOR=#800000]"y"[/COLOR][/SIZE][SIZE=2], strRefTableLoc)
[/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]For
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]e.Graphics.DrawLine(Pens.Black, locationMain, locationSecond)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

Ok. What i do here is, i have a dataset with the first column as the name of the Main table, the second as it's location and the thirt as the name of the table it references. First i check that no main table references itself.
So then i take the table name and i have a function (pointconvertor) that takes it's cordinates (the Locationmain and locationsecond are declared Private somewhere in my code).
Then i try to take the location of the referenced table. The "arrayTablesInForm1" is an array that contain all the tables that are added in the mainform. I look in the array and if the table exists then i take it's coordinates and i exit my inside for.
Then i draw the 2 tables reference line.....

So the problems i have.....
The lines are drawn but some of them are shorter and misplaced. I've checked the Location coordinates and they seem to be right for the tables.
Also if i maximize and then restore the form, the lines become different (i don't move any forms). I must note that because the relationships i get from the tables are from both primary and foreign, some relationships lines are draw again (the same line coordinate above another drawn line).
Any thoughts? :(
 
Ok. My code seems to work now.
I included a "me.refresh" after i add a form to the mainform and it goes well as far as i can tell but any comments or suggestions are very welcome.
 
Back
Top