none
Drawing Clipping RRS feed

  • Question

  • I have generated some code that takes points from a comma delimited file and plots them on a form.

    Problem is the default form size is 300 x 300 and, although I have resized the form to 1000 x 300 the plot stops at x = 300 as if it is still seeing the original form size.

    Is there some hidden parameter that is defining the drawing extent.

    I have tried the Graphics.Clip method to define a 1000 x 300 rectangle to draw in but the line still stops at 300.

    I am using VB 2015

    Any ideas?

    Thursday, March 1, 2018 9:28 PM

Answers

  • Oh, that's because you're getting the graphics instance before the form is finished loading and setting itself to the specified size.

    You can't really do drawing that way.  Try handing the Paint event on the form instead.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by BotallackDave Friday, March 2, 2018 9:41 PM
    Friday, March 2, 2018 9:06 PM
    Moderator

All replies

  • Please show a sample of the code you are using to plot the points.  Without seeing how you are plotting the points it is impossible to say where the issue might be.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, March 1, 2018 9:41 PM
    Moderator
  • Here is the code; for what it's worth.

    I can replicate the problem by simply drawing a horizontal line of more than 300 pixels.

    Public Class Form1
        Dim bar = 0
        Dim length As Integer = 10
        Dim incline As Integer = 200
        Dim Old_length As Integer = 10
        Dim Old_incline As Integer = 200
        Dim myGraphics As Graphics = Me.CreateGraphics
        Dim rect1 As New Rectangle(10, 10, 1000, 200)
        Dim WhitePen = New Pen(Brushes.White, 2)
        Dim BlackPen = New Pen(Brushes.Black, 2)
        Dim GreenPen = New Pen(Brushes.Green, 2)
        Dim BluePen = New Pen(Brushes.Blue, 2)
        Dim RedPen = New Pen(Brushes.Red, 2)



        Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
            length = 10
            incline = 200
            Old_length = 10
            Old_incline = 200
            Timer1.Enabled = True
            Using MyReader As New Microsoft.VisualBasic.
                            FileIO.TextFieldParser(
                              "C:\TestFolder\test.txt")
                MyReader.TextFieldType = FileIO.FieldType.Delimited
                MyReader.SetDelimiters(",")
                Dim currentRow As String()
                While Not MyReader.EndOfData
                    Try
                        currentRow = MyReader.ReadFields()
                        Dim currentField As Integer
                        Dim field As Integer = 0
                        For Each currentField In currentRow
                            If field = 0 Then
                                Old_length = length
                                length = Old_length + currentField
                                field = 1
                            Else
                                Old_incline = incline
                                incline = Old_incline - currentField
                            End If
                        Next
                        If Old_incline > incline Then
                            Call Draw_Line(RedPen, Old_length, Old_incline, length, incline)
                        ElseIf Old_incline = incline Then
                            Call Draw_Line(WhitePen, Old_length, Old_incline, length, incline)
                        Else
                            Call Draw_Line(GreenPen, Old_length, Old_incline, length, incline)
                        End If
                        Call Draw_Minute_Mark()

                    Catch ex As Microsoft.VisualBasic.
                      FileIO.MalformedLineException
                        MsgBox("Line " & ex.Message &
            "is not valid and will be skipped.")
                    End Try
                End While
            End Using

        End Sub

        Sub Draw_Line(Pen, Old_length, Old_incline, length, incline)
            myGraphics.SetClip(rect1)
            myGraphics.DrawLine(Pen, Old_length, Old_incline, length, incline)


        End Sub
        Sub Draw_Minute_Mark()
            For i As Integer = 0 To 100
                Call Draw_Dashed_Line(i * 60)
            Next
        End Sub
        Sub Draw_Dashed_Line(x_coord)
            For i As Integer = 0 To 19
                myGraphics.DrawLine(BluePen, x_coord, 20 * i, x_coord, (20 * i) + 10)
                myGraphics.DrawLine(BlackPen, x_coord, (20 * i) + 10, x_coord, (20 * i) + 20)
            Next

        End Sub

        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Call Draw_Line(BlackPen, 10 + bar, 0, 10 + bar, 200)
            bar = bar + 10
            Call Draw_Line(WhitePen, 10 + bar, 0, 10 + bar, 200)

        End Sub
    End Class

     
    Friday, March 2, 2018 8:44 PM
  • Oh, that's because you're getting the graphics instance before the form is finished loading and setting itself to the specified size.

    You can't really do drawing that way.  Try handing the Paint event on the form instead.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by BotallackDave Friday, March 2, 2018 9:41 PM
    Friday, March 2, 2018 9:06 PM
    Moderator
  • Many thanks

    Problem solved = now my plot goes to the full extent of the form.

    Friday, March 2, 2018 9:44 PM