Farzin Kanzi
Farzin Kanzi

Reputation: 3435

Drawing a simple Line in user controls

I guess my question is very simple but I did not find any answer.

I have a vb.Net project with a MainForm and a Button(btn1) and a UserControl(uc1).

All I want is when I click on btn1 a simple line draw on uc1 using graphics class. thanks.

Upvotes: 2

Views: 2416

Answers (1)

Matt Wilko
Matt Wilko

Reputation: 27322

Create a method in your UserControl to draw the line:

Public Class MyUserControl
    Public Sub DrawLine()
        Using g As Graphics = Me.CreateGraphics
            g.DrawLine(Pens.Blue, New Point(10, 10), New Point(100, 50))
        End Using
    End Sub
End Class

Then call this from your button click:

Private Sub Button26_Click(sender As Object, e As EventArgs) Handles Button26.Click
    MyUserControl1.DrawLine()
End Sub

This is a simple solution, but you may want to do all your drawing in the paint event of the usercontrol

Upvotes: 3

Related Questions