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

 
hmmm u were right i increased the size and it works fine now but i dont get it
the length of the myPicArea wass equal to the length of the line then y werent they visible. does it need extra space?>
 
hmm, k one last thing, isnt there any other way i can draw these lines, i mean y is it necessary to call my function from pain event, isnt there any way i can make these lines permanant like labels
 
I suggested the paint event because you were initially drawing on the form.
The code you presented above with the Paint event is not good practice because you are retreiving the data each time the paint event occurs. You only need to retreive it once, unless you expect it to change frequently.

To draw a "permanent" graph, I would suggest using a pictureBox control.
You would create a Bitmap the size of the pictureBox, create a graphics object from the bitmap, draw on the bitmap using the graphics object, then assign the Bitmap as the Image property of the pictureBox.

Here some sample code:
VB.NET:
Public Sub drawGraph(ByVal arrGraph() As Double)
    Dim bm As Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    Dim g As Graphics = Graphics.FromImage(bm)
    Dim i As Integer = 0
    Dim x As Integer = 0
    Dim y As Integer = 0
    For i = 0 To (countrec - 1)
        g.DrawLine(Pens.Black, 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
    PictureBox1.Image = bm
    'clean up resources
    g.Dispose()
End Sub
 
Back
Top