Solved

Creating dynamic handlers

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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