withevents addhandler

danyeungw

Well-known member
Joined
Aug 30, 2005
Messages
73
Programming Experience
10+
I don't know how to edit my post. I have one more question. If I would like add 10 buttons on the form programmaticall, how do I create a loop to get Friend WithEvents Button1 as New Button, Friend WithEvents Button2 as New Button... ? How do I write code in class level?

Second, if I want to create click event using the following:

AddHandler btn0.click, addressof ClickHandler
Private Sub ClickHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
'put codes here
End Sub

1. I got syntex error on AddHandler. What was wrong?
2. How do I use a loop to create AddHandler btn0.click, addressof clickHandler, AddHandler btn1.click, addressof clickHandler, ...

Thanks.
DanYeung

ps I saw the Edit button after I posted this.
 
Last edited:
Post unrelated question in separate threads. Post was moved.

1. The syntax of your example is correct, but you have to put that statement inside a sub or function method. Placing it at class level gives syntax error.

2:
VB.NET:
Dim btn As Button
For i As Short = 0 To 9
  btn = New Button
  btn.Name = "btn" & i.ToString
  btn.Text = i.ToString
  'btn.Location = ....
  AddHandler btn.Click, AddressOf ClickHandler
  Me.Controls.Add(btn)
Next
 
Back
Top