Reputation: 59
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Panel1.Controls.Clear()
Dim patiekalai = New Button()
Panel1.Controls.Add(patiekalai)
patiekalai.Location = New Point(0, 0)
patiekalai.Size = New Size(80, 50)
patiekalai.Image = Image.FromFile("../M/Karštieji patiekalai.jpg")
Private Sub Patiekalai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Patiekalai.Click
I get error while trying to add a Click event handler for my dynamically created button patiekalai:
Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
Upvotes: 1
Views: 516
Reputation: 5545
You can't do this because the button does not exist until Button1 is clicked, at run-time. What you want to do is add a handler as well at run-time.
Remove the "Handles" from the "Patiekalai_Click" method. Then, after you create your control at run-time: "patiekalai.Image = Image...." add this line
AddHandler patiekalai.Click AddressOf Patiekalai_Click
This tells the click event of the button to call the selected method.
Upvotes: 1