Solved

Creating dynamic handlers

Posted on 2010-08-29
4
406 Views
Last Modified: 2012-05-10
Hey there. I'm attempting to call a user defined function (callbyname) on a dynamically created button using add handler.

AddHandler button.Click, AddressOf CallByName(form, "function_name", CallType.Method)

Open in new window


unfortunatly i keep getting this error:
'AddressOf' operand must be the name of a method (without parentheses)

Any help would be great :)

Many Thanks
Ben
0
Comment
Question by:lazypeople
  • 2
4 Comments
 
LVL 47

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 50 total points
ID: 33552946
The routine you use in AddHandler *must* have the same signature as the event. So for a button's Click event, the routine will need to have an Object (sender) and EventArgs (e), like this....

    AddHandler button.Click, AddressOf Button_Click

...where the Button_Click routine looks like this....

    Public Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
        'your code
    End Sub

Wayne
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 200 total points
ID: 33552975
Hi Ben;

Try it like this.

' Create the Button
Dim dButton As New Button()
dButton.Name = "DynamicButton"
dButton.Text = "DynamicButton"
dButton.Location = New Point(10, 10)
'...
'Assign the Click event to an event handler
AddHandler dButton.Click, AddressOf DynamicButton_Click
' Add Button to the forms collection
Me.Controls.Add(dButton)




Private Sub DynamicButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    ' Call the function to CallByName here in the event handler
    ' CallByName(form, "function_name", CallType.Method)

End Sub

Fernando
0
 
LVL 3

Author Closing Comment

by:lazypeople
ID: 33553230
Thanks :)
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33553245
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now