need help with graphics draw line

hawkseye

Active member
Joined
Dec 4, 2004
Messages
28
Programming Experience
Beginner
hello,

im drawing a line on a form with has a couple of labels now the first problem that im having is that the line when drawn goes behind the labels whereas i want it to be over them

second when i scroll the page the line repeats itself showing multiple lines i dont want that to happen

the code that im using is

Public Sub DrawGWT(ByVal yVal As Integer)

Dim g As Graphics = Me.CreateGraphics()





Dim myPen As New Pen(Color.Blue, 2)

g.DrawLine(myPen, 12, 344 + yVal, 100, 344 + yVal)

End Sub



please help me

 
If you want the line to be over the label, you have to draw on the label. In your example, you're drawing on the form which is behind the label.

You'll also want to look at the Paint event for controls. If the graphics object becomes invalid (the control is moved off screen for example) the next time the paint event is triggered, you'll get the default painting instead of the line.

To stop the repeating of the line, in the load event handler use these statements:
VB.NET:
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
 
Probably the easyest way would be to have a control over the labels and draw in that.
 
If you have a control over the label, that control would block the label.

The CreateGraphics method is a member of the base Control class. Both forms and labels are derived from the control class. So instead of doing Me.CreateGraphics, try Label1.CreateGraphics (where Label1 is the name of the label of course).

And remember to look into the Paint event of the control. It's always best to do custom painting in the Paint event handler.
 
not if the background color is set to invisible. He has multiple lables, so painting on all of them will be a hassle.
 
hello again

actually what i did was i moved my drawing to the place where the were no labels :p now that solved the problem bt now another another ones come up. When i scrool the page dawn the linemove down with the scroll. is there any way to fix them (i mean they should stay where oridionally drawn). i know it moves with the paint event and everytime i scrool that event is call but dont know how to fix it :(
 
hello

well i fugured out that the graph isnt moving down its actually darawn when a scroll. but the problem is that the last thee line are drawn but they dont show up on the screen
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

Me.SqlConnection1.Open()

Dim i As Integer = 0

Dim arrGraph(countRec) As Double

Dim sqlRdr As SqlDataReader

Me.SqlSeland5.Parameters(0).Value = holeIdq

sqlRdr =
Me.SqlSeland5.ExecuteReader()

Try

While sqlRdr.Read

arrGraph(i) =
CType(sqlRdr.Item(0), Integer)

i = i + 1

End While

Catch erroronInsert As Exception

MessageBox.Show(erroronInsert.ToString)

End Try

Me.SqlConnection1.Close()

drawGraph(arrGraph)

End Sub


Public Sub drawGraph(ByVal arrGraph() As Double)

Dim g As Graphics

Dim myPen As New Pen(Color.Black, 1)

Dim i As Integer = 0

Dim x As Integer = 0

Dim y As Integer = 0

For i = 0 To (countRec - 1)

Me.myPicArea.CreateGraphics.DrawLine(myPen, x, y, CInt(arrGraph(i) * 3), y + 40)

Console.WriteLine(x & "," & y & "," & arrGraph(i) * 3 & "," & y + 40)

x =
CInt(arrGraph(i) * 3)

y = y + 40

Next



Me.Controls.Add(Me.myPicArea)

End Sub


please check whats wrong
 
ya it is

ya it is big enough, it the end of myPicArea (where the last three lines should show) stays empty but when i scroll up n down i catch a slight view of the lines but then they disapear.
 
Back
Top