Solved

Creating dynamic handlers

Posted on 2010-08-29
4
412 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 63

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 63

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

840 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