?
Solved

Creating dynamic handlers

Posted on 2010-08-29
4
Medium Priority
?
420 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 47

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 200 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 800 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

762 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