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

 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
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)
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Probably the easyest way would be to have a control over the labels and draw in that.
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
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.
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
not if the background color is set to invisible. He has multiple lables, so painting on all of them will be a hassle.
 

hawkseye

Active member
Joined
Dec 4, 2004
Messages
28
Programming Experience
Beginner
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 :(
 

hawkseye

Active member
Joined
Dec 4, 2004
Messages
28
Programming Experience
Beginner
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
 

hawkseye

Active member
Joined
Dec 4, 2004
Messages
28
Programming Experience
Beginner
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.
 

hawkseye

Active member
Joined
Dec 4, 2004
Messages
28
Programming Experience
Beginner
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?>
 

hawkseye

Active member
Joined
Dec 4, 2004
Messages
28
Programming Experience
Beginner
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
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
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
 
Top Bottom