Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drawing Graphics

Status
Not open for further replies.

mechInferno

Technical User
Oct 24, 2005
24
CA
Hi, I have been working on a project for a class project which works similar to MS project. I have been trying to draw lines on a label (just to test) for a timeline. I can get the lines drawn, but when I scroll left to right on the form, the lines disappear. Is there anyway to make them stay or refresh the form when the scroll moves?

Kevin
 
Write a sub that paints the label, like:
Code:
    Private Sub PaintMyLabel()
        Dim g As Graphics = Label1.CreateGraphics
        g.DrawLine(Pens.Black, 0, 0, Label1.Width, Label1.Height)
    End Sub
and call it also in the paint event, like:
Code:
Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
    PaintMyLabel()
End Sub

Hope this helps
 
Well I kind of use this at this point.

Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
'get the difference of weeks between todays date and the selected date.
diff = DateDiff(DateInterval.WeekOfYear, today, future)
start = DateDiff(DateInterval.WeekOfYear, mbegin, today).tostring
DrawPhase(start * 25, 65, diff * 25) 'Draws the line at points for length of weeks; not to scale.

End Sub

Public Sub DrawPhase(ByVal x As Integer, ByVal y As Integer, ByVal len As Integer)
Dim Xval As Integer = x
Dim Yval As Integer = y
Dim l As Integer = len

Dim myPen As New Pen(Color.Red, 3) 'new pen object
Dim g As Graphics = Label2.CreateGraphics 'new graphics object
g.DrawLine(myPen, Xval, Yval, Xval + l, Yval) 'draws the line with pen and coordinates
End Sub

This is what I have and it draws a line with coordinates and a length. But, my problem occurs when I run the program, I have the autocroll on and when the line is drawn, the line disappears when I scroll the form. I just don't know how to keep the line there when I scroll back. If I push the draw button again it shows, I want it jsut to stay.

Thanks kevin
 
Hi,
you did not seem to have read my post. Once more:

Code:
    Private btnDraw_pressed As Boolean = False

    Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles btnDraw.Click

        ' indicate that from now on, the label (after the button click) should be re-painted
        btnDraw_pressed = True
        ' refreshing the form will cause all objects to be re-painted
        Me.Refresh()

    End Sub

    Public Sub DrawPhase(ByVal x As Integer, ByVal y As Integer, ByVal len As Integer)

        Dim Xval As Integer = x
        Dim Yval As Integer = y
        Dim l As Integer = len

        Dim myPen As New Pen(Color.Red, 3)              'new pen object
        Dim g As Graphics = Label2.CreateGraphics       'new graphics object
        'draws the line with pen and coordinates
        g.DrawLine(myPen, Xval, Yval, Xval + l, Yval)

    End Sub

    Private Sub Label2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
    Handles Label2.Paint

        If btnDraw_pressed Then

            'get the difference of weeks between todays date and the selected date.
            diff = DateDiff(DateInterval.WeekOfYear, Today, future)
            start = DateDiff(DateInterval.WeekOfYear, mbegin, Today).tostring
            'Draws the line at points for length of weeks; not to scale.
            DrawPhase(start * 25, 65, diff * 25)

        End If

    End Sub


Hope this now helps.
Just copy and paste it.
 
Thanks that does help. Thats exactly what I was trying to get.

Thanks again

Kevin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top