Resolved Graphics line length

Rumporheum

New member
Joined
Feb 9, 2021
Messages
4
Programming Experience
1-3
I'm a beginning VB 2010 user. I have a windows form of width 1000, height 600. I am drawing a line at the top of the form starting at x1 = 0 to x2 = 275. This works. However, when I increase the length of the line to x1 = 0 to x2 = 350, the length does not increase. Is there a limit to the length of lines in VB 2010. Also, if I start the line at x1 = 300 to x2 = 500, the line is not drawn.
 
Solution
basic example:
Private Sub MyBox_Paint(sender As Object, e As PaintEventArgs) Handles MyBox.Paint
    Using myTrackPen = New Pen(Color.Blue, 10), myThruPen = New Pen(Color.Red, 10)
        e.Graphics.DrawLine(myTrackPen, 0, 30, 80, 30)
        e.Graphics.DrawLine(myTrackPen, 110, 30, 190, 30)
        e.Graphics.DrawLine(myTrackPen, 110, 50, 190, 50)
        e.Graphics.DrawLine(myThruPen, 80, 30, 110, 30)
        If drawit Then
            e.Graphics.DrawLine(myThruPen, 80, 30, 110, 50)
        End If
    End Using
End Sub

Private drawit = False

Private Sub ButtonAction_Click(sender As Object, e As EventArgs) Handles ButtonAction.Click
    drawit = True
    Refresh()
End Sub
If it's not working then you did it wrong. If you don't show us what you did then we can't tell you what's wrong with it. Some general advice:
  • Do all your GDI+ drawing in the Paint event handler of the form or control you want to draw on, using the Graphics object it provides.
  • Store the data that represents your drawing in one or more member variables.
  • When you want to change the drawing, update the relevant member variable(s) and then call Invalidate on the appropriate form or control to prompt a Paint event.
  • You can call Invalidate without arguments but, particularly if you are raising many Paint/ICODE] events, it is more efficient to calculate the smallest [ICODE]Rectangle or Region that contains the area that has or may have changed and pass that.
 
Thank you for your reply. I'm studying your suggestions, and if I need further clarification, I'll let you know.

If it's not working then you did it wrong. If you don't show us what you did then we can't tell you what's wrong with it. Some general advice:
  • Do all your GDI+ drawing in the Paint event handler of the form or control you want to draw on, using the Graphics object it provides.
  • Store the data that represents your drawing in one or more member variables.
  • When you want to change the drawing, update the relevant member variable(s) and then call Invalidate on the appropriate form or control to prompt a Paint event.
  • You can call Invalidate without arguments but, particularly if you are raising many Paint/ICODE] events, it is more efficient to calculate the smallest [ICODE]Rectangle or Region that contains the area that has or may have changed and pass that.
 
I have a group box configured as such:

Public Sub gbxMyBox_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)

Dim myGraphics As Graphics = gbxMyBox.CreateGraphics
Dim myTrackPen As Pen
Dim myThruPen As Pen
Dim mySwtPen As Pen

myTrackPen = New Pen(Drawing.Color.Blue, 10)
myThruPen = New Pen(Drawing.Color.Red, 10)
mySwtPen = New Pen(Drawing.Color.Red, 10)

myGraphics.DrawLine(myTrackPen, 0, 30, 80, 30)
myGraphics.DrawLine(myTrackPen, 110, 30, 190, 30)
myGraphics.DrawLine(myTrackPen, 110, 50, 190, 50)
myGraphics.DrawLine(myThruPen, 80, 30, 110, 30)

End Sub

I also have a button in this group box, that when clicked, I intend to alter a line specified in the above code. I tried this:

Private Sub btnAction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

myGraphics.DrawLine(mySwtPen, 80, 30, 110, 50)

End Sub

Before I can run the code, I get this complaint: myGraphic (and mySwtPen) not declared.

Is it that the button is not part of the group box? I dragged the button from the toolbar to the group box.
 
Dim myGraphics As Graphics = gbxMyBox.CreateGraphics
All painting must be done in Paint event using e.Graphics. If you need to change painting you must use variables declared outside methods, these can be set in click method that also must call Refresh for painting to update.
 
insertcode.png
 
basic example:
Private Sub MyBox_Paint(sender As Object, e As PaintEventArgs) Handles MyBox.Paint
    Using myTrackPen = New Pen(Color.Blue, 10), myThruPen = New Pen(Color.Red, 10)
        e.Graphics.DrawLine(myTrackPen, 0, 30, 80, 30)
        e.Graphics.DrawLine(myTrackPen, 110, 30, 190, 30)
        e.Graphics.DrawLine(myTrackPen, 110, 50, 190, 50)
        e.Graphics.DrawLine(myThruPen, 80, 30, 110, 30)
        If drawit Then
            e.Graphics.DrawLine(myThruPen, 80, 30, 110, 50)
        End If
    End Using
End Sub

Private drawit = False

Private Sub ButtonAction_Click(sender As Object, e As EventArgs) Handles ButtonAction.Click
    drawit = True
    Refresh()
End Sub
 
Solution
All painting must be done in Paint event using e.Graphics. If you need to change painting you must use variables declared outside methods, these can be set in click method that also must call Refresh for painting to update.
Thanks for your help. Your advice led me to success.
 

Latest posts

Back
Top