locked
drawing a line on a form RRS feed

  • Question

  • In  the goal of drawing fast lines, i tested this on a form and button:

    Public myPen As Pen
    Public n As Integer
    Public x, length, xprec As Long
    Public myGraphics As Graphics = Me.CreateGraphics
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            length = 2000
    
            myPen = New Pen(Drawing.Color.Red, 1)
    
            For n = 0 To length - 1
                x = n
                myGraphics.DrawLine(myPen, n, xprec, n + 1, x)
                xprec = x
            Next
    
            myPen.Dispose()
            myGraphics.Dispose()
    End Sub
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Width = 500
            Me.Height = 500
    End Sub

    The result should be a line from top left to down right. But it stops at some point. It seems that the area for drawing is limited

    to up left. Why? If i resize to much less i get the line running well, but i'm cheeting.

    Enzo




    Saturday, April 11, 2020 1:56 PM

Answers

  • Now that i have reached what i wanted, I let know what i've done. Key point was putting:

     Dim myGraphics As Graphics = Me.CreateGraphics

    inside sub button and using paint event to clear.

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim myGraphics As Graphics = Me.CreateGraphics
            Me.Invalidate()
            Timer1.Enabled = True
            length = Me.Width
            myPen = New Pen(Drawing.Color.Blue, 4)
            xprec = Me.Height / 2
            For n = 0 To Me.Width
                x = 100 * Math.Sin(20 * 3.141 * n / length) + Me.Height / 2
                While timer_end = False
                    Application.DoEvents()
                End While
                myGraphics.DrawLine(myPen, n, xprec, n + 1, x)
                timer_end = False
                xprec = x
                Me.Text = n
            Next
    
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            timer_end = True
        End Sub
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Width = 300
            Me.Height = 300
        End Sub
    
        Private Sub Form3_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    
            e.Graphics.Clear(Color.AliceBlue)
    
        End Sub





    Saturday, April 11, 2020 9:43 PM

All replies

  • If you want to draw a line from top left to down right, you can just do :

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        e.Graphics.DrawLine(New Pen(Color.Red, 2), 0, 0, Me.ClientSize.Width, Me.ClientSize.Height)   
    End Sub

    +

    on Form1_Load :

    Me.ResizeRedraw = True

    If you want to draw a lot of lines or other things, use Double Buffering 

    • Edited by Castorix31 Saturday, April 11, 2020 4:46 PM
    • Proposed as answer by tommytwotrain Sunday, April 12, 2020 4:11 PM
    Saturday, April 11, 2020 4:37 PM
  • But i want to draw when i want, not when i load the form.

    I have to update the line time after time, as new data arrive. Think to a timer, when expires a new line must be displayed.

    Anyway, why my software creates a line confined in an area?

    In this example I used i timer to update the line, but it stops at some point.

    Saturday, April 11, 2020 5:22 PM
  • But i want to draw when i want, not when i load the form.

    It is not when you load the Form, it is in Paint event

    All drawing in Windows is done in Paint event (WM_PAINT)

    Then you force a repaint on demand by invalidating the window (timer if you want, thread, etc...)

    • Proposed as answer by tommytwotrain Sunday, April 12, 2020 4:11 PM
    Saturday, April 11, 2020 5:59 PM
  • but my question remains, why my program does not work? There must be an answer. Something is drawn without any paint event.

    Anyway, following your advice, loading the form a paint event happens, so what must i do?

    Can you write an example that creates a line when pressing a button?

    I'have done a test:

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Enabled = True
            abilita = True
            length = 2000
    
            myPen = New Pen(Drawing.Color.Red, 16)
            xprec = 0
            For n = 0 To Me.Width
                x = n
                While timer_end = False
                    Application.DoEvents()
                End While
                timer_end = False
                xprec = x
                Me.Text = n
                Me.Invalidate()
            Next
    
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            timer_end = True
        End Sub
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Width = 500
            Me.Height = 500
        End Sub
    
        Private Sub Form3_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    
            If abilita = True Then
                myGraphics.DrawLine(myPen, n, xprec, n + 1, x)
            End If
    
        End Sub

    this is the result:

    Saturday, April 11, 2020 6:38 PM
  • If you want to draw some non-persistent lines outside the Paint event, then try creating the Graphics object inside the function using Me.CreateGraphics. Remove the myGraphics and other members. Use local variables when possible.

    But inside the Paint event use e.Graphics instead of Me.CreateGraphics.

    • Proposed as answer by tommytwotrain Sunday, April 12, 2020 4:11 PM
    Saturday, April 11, 2020 7:11 PM
  • Hy Viorel,

    changing

    myGraphics.DrawLine(myPen, n, xprec, n + 1, x)

    to

    e.Graphics.DrawLine(myPen, n, xprec, n + 1, x)

    inside paint event the signal travels all the form!

    But it is not solid, when it draws a point, delete the former. What must i do?

    Saturday, April 11, 2020 7:26 PM
  • Now that i have reached what i wanted, I let know what i've done. Key point was putting:

     Dim myGraphics As Graphics = Me.CreateGraphics

    inside sub button and using paint event to clear.

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim myGraphics As Graphics = Me.CreateGraphics
            Me.Invalidate()
            Timer1.Enabled = True
            length = Me.Width
            myPen = New Pen(Drawing.Color.Blue, 4)
            xprec = Me.Height / 2
            For n = 0 To Me.Width
                x = 100 * Math.Sin(20 * 3.141 * n / length) + Me.Height / 2
                While timer_end = False
                    Application.DoEvents()
                End While
                myGraphics.DrawLine(myPen, n, xprec, n + 1, x)
                timer_end = False
                xprec = x
                Me.Text = n
            Next
    
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            timer_end = True
        End Sub
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Width = 300
            Me.Height = 300
        End Sub
    
        Private Sub Form3_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    
            e.Graphics.Clear(Color.AliceBlue)
    
        End Sub





    Saturday, April 11, 2020 9:43 PM
  • Ill see all and i really hope you are right!

    But when i asked for my problems i did not have any satisfactory answer.

    Enzo

    Sunday, April 12, 2020 3:39 PM
  • Ill see all and i really hope you are right!

    But when i asked for my problems i did not have any satisfactory answer.

    Enzo

    Its complicated.

    No one understands your question. Many dont know what a log is let alone why the chart does not work with no points in the series.

    You need to get more basic. Make a simple example of only one problem that others can run.

    Your questions are being answered by Castor, by Les, By Viorel. But you dont understand the basics enough to understand the answers.

    You need to start simple. Work many simple examples. Don't just post a bunch of code you think should work but does not. The forum members have lots to do.

    :)

    You need to get simpler and then go one step at a time. Get your simple example running first. Then add one thing and get that running.

    What you are doing is just many dominos of errors so you cant get it to run and still dont understand it.

    Use the paint event to draw is the first thing you need to learn. Learn what e.graphics is. You are mixing things you dont completely understand.

    Leave the mouse out for now.

    Everyone goes through it. It is hard.

    I no longer frequent the forum.



    Sunday, April 12, 2020 4:07 PM