Search code examples
vb.netdrawingsystem.drawingpaintevent

Create a line when press a button


I want to make an drawing program "in real time". I have two text boxes and a button in my form. I want to enter two values in the text boxes, push the button and the draw the line.

I tried to call from the b_Click (b is the button) the Form_paint but don't know what to do with the sender and the e parameters.

Is there any way to do this?

Public Class Form1
Dim a, c, d, e As Integer



Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    Dim pen As New Pen(Color.FromArgb(255, 0, 0, 0))
    e.Graphics.DrawLine(pen, a, c, 300, 100)
End Sub

Private Sub b_Click(sender As Object, e As EventArgs) Handles b.Click
    a = t1.Text
    c = t2.Text
    Form1_Paint(0,??)

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    a = 0
    c = 0
  End Sub
  End Class

Solution

  • You should call the Refresh method of the Form, not call the event handler yourself. That method will eventually invoke the Paint event and its handler.

    Private Sub b_Click(sender As Object, e As EventArgs) Handles b.Click
        a = t1.Text
        c = t2.Text
    
        Refresh()
    End Sub