Question

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.

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top