unable to draw line

alaric

Well-known member
Joined
Oct 12, 2005
Messages
53
Programming Experience
Beginner
[RESOLVED] unable to draw line

Hi,

I know how to draw a line
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]
Dim[/COLOR][/SIZE][SIZE=2] DrawLine [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Drawing.Graphics
DrawLine = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].CreateGraphics[/SIZE]
[SIZE=2][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] PenColor [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Drawing.Pen(System.Drawing.Color.Black)
[/SIZE][/SIZE]

now I want to position this line just above a label (which is in a groupbox)
so I thought:
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]
[SIZE=2][COLOR=#008000]'get location X,Y of the groupBox)
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] posX1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].grpFactAanmaak.Location.X
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] posY1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].grpFactAanmaak.Location.Y
[/SIZE][SIZE=2][COLOR=#008000]'get location X,Y of the label)
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] posX2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lblCurTotaal.Location.X
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] posY2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lblCurTotaal.Location.Y
[/SIZE][SIZE=2][COLOR=#008000]'Me.lblCurTotaal.Location = New System.Drawing.Point(120, 160)
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'calc the start X, Y pos of the line
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ActposX [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = posX2 + posX1
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ActposY [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = posY2 + posY1
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DrawLine [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Drawing.Graphics
DrawLine = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].CreateGraphics
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] PenColor [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Drawing.Pen(System.Drawing.Color.Black)[/SIZE]
[SIZE=2][COLOR=#008000]'draw the line 10 px above the label and length 30 px[/COLOR]
DrawLine.DrawLine(PenColor, ActposX, ActposY - 10, ActposX + 30, ActposY - 10)
[/SIZE][/COLOR][/SIZE]

But there's NO line!:confused:

what Im I doing wrong here?

thanks in advance
 
Last edited:
thanx for your quick reply and your suggestion

iwas doing this in the form's paint event?
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]
Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] frmInschrijven_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]MyBase[/COLOR][/SIZE][SIZE=2].Paint
[/SIZE]

i changed it to
VB.NET:
Expand Collapse Copy
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] grpFactAanmaak_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] grpFactAanmaak.Paint
[/SIZE]

but now my form is not displayed completeley, looks like its rendering or something...
 
sorry for the inconvenience but I changed the code to:
VB.NET:
Expand Collapse Copy
[COLOR=#0000ff]
Dim[/COLOR] L1 [COLOR=#0000ff]As[/COLOR] System.Drawing.Graphics
L1 = [COLOR=#0000ff]Me[/COLOR].CreateGraphics
[COLOR=#008000]'Use a red pen color to draw a line
[/COLOR][COLOR=#0000ff]Dim[/COLOR] PenColor [COLOR=#0000ff]As[/COLOR][COLOR=#0000ff]New[/COLOR] System.Drawing.Pen(System.Drawing.Color.Black, 10)
L1.DrawLine(PenColor, ActposX, ActposY - 10, ActposX + 30, ActposY - 10)

i used drawline instead of L1 for the linevariable. drawline is (offcourse) not valid.

Now the form comes up, but No line in the groupbox.
these are the calculated coordinates (console.writeline)
actposx -> 576
actposy -10 ->586
actposx +30 -> 606
actposy -10 -> 586
 
Sounds like rather big group box to me. Are you using the co-ordinates for the label in relation to the form, or it's co-ordinates relative to it's container i.e the groupbox.
 
you are right.
The first time I didnt see my line, I thought I calculate the coordiantes like start X, Y of the grpBox and add start X,Y of the label inside this groupbox , in order to calc the startingpoint of my line. this was when i used the paintevent of the form

now i use the paint event of the grpbox.

but whats the best/easiest way?
if I change the coordinates rel to the grpbox... still no line
 
This is something that i observe many people do. What you have in the paint event is a graphics object that is ready to draw on so you don't need to create a new graphics object.


VB.NET:
Expand Collapse Copy
ByVal e as system.blah, blah painteventargs

So it should be

VB.NET:
Expand Collapse Copy
e.graphics.drawline(etc.....)

With me?
 
thanks so far,

i think I lost you. Do you mean that I dont need a the sub
VB.NET:
Expand Collapse Copy
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#000000] grpFactAanmaak_Paint([/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2][COLOR=#000000] sender [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2][COLOR=#000000], [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2][COLOR=#000000] e [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Windows.Forms.PaintEventArgs) [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#000000] grpFactAanmaak.Paint[/COLOR]
[/SIZE][/COLOR][/SIZE]

but just instead
(just somewhere ie load form event)?

i tried (on the form load event):
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]
Dim[/COLOR][/SIZE][SIZE=2] posX2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lblCurTotaal.Location.X
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] posY2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Single[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lblCurTotaal.Location.Y
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] L1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Drawing.Graphics
L1 = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].CreateGraphics
[/SIZE][SIZE=2][COLOR=#008000]' 'Use a red pen color to draw a line
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] PenColor [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Drawing.Pen(System.Drawing.Color.Black)
L1.DrawLine(PenColor, posX2, posY2 - 10, posX2 + 30, posY2 - 10)
[/SIZE]

still no luck
it seems that I just dont understand the techniek:(
 
Yes you do need your groupbox paint sub. This handles all the painting for the control. So you need to put all your code for painting in there so it will go something like this......

VB.NET:
Expand Collapse Copy
PrivateSub grpFactAanmaak_Paint(ByVal sender AsObject, ByVal e As System.Windows.Forms.PaintEventArgs) Handles grpFactAanmaak.Paint
 
Dim posX2 AsSingle = Me.lblCurTotaal.Location.X
Dim posY2 AsSingle = Me.lblCurTotaal.Location.Y
' 'Use a red pen color to draw a line
Dim PenColor AsNew System.Drawing.Pen(System.Drawing.Color.Black)
e.Graphics.DrawLine(PenColor, posX2, posY2 - 10, posX2 + 30, posY2 - 10)
 
Back
Top