Optionally AddHandler

Is there a way that I can optionally add an event handler ?

Below is something like what I'd like to accomplish.  The obvious problem here is that if the button does not require a Handler then there won't be a btn_Click method.  Any ideas ?

Public Sub addBtn(content As Object, name As String, hasHandler As Boolean)
        Dim btn As New Button
        btn.Content = content
        btn.Name = name
        If hasHandler Then
            AddHandler btn.Click, AddressOf btn_Click
        End If
        ContentPanel.Children.Add(btn)
    End Sub

Open in new window

WatersideAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Well what you show is exactly how you would do it. Can I assume that you've a set of predefined handler functions declared in your code? If not, then how are you declaring the handlers?
0
WatersideAuthor Commented:
Ah there lies the problem.  If hasHandler is False then there won't be a btn_Click.
0
käµfm³d 👽Commented:
Understood (somewhat), but if hasHandler is False, then you won't enter that block anyway. So I'm missing something here.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

WatersideAuthor Commented:
The compiler can't find the handler.
0
käµfm³d 👽Commented:
In theory, I understand what you are saying, but based on the code example that you are displaying, this wouldn't happen. A handler can't exist at one moment in code and then not exist at another moment. If there is a function called btn_Click in your code when you compile, then it's always going to be there regardless of what the If is doing. So I'm thinking that your example isn't doing the question justice. If I'm mistaken, then please let me know, but maybe a different example is needed.

Now, if you were using delegates or labmdas, then I could see where you might have this issue.

Can you maybe rephrase either the question or the example?
0
Jacques Bourgeois (James Burger)PresidentCommented:
Same as kaufmed, your question might not be clear because your code does exactly what you seem to be asking for.

Maybe what you intend to say it that the event fires even your hasHandler parameter is false and you do not set the handler through your code. This would be the case if you created the Click event in the usual way, but did not remove the Handles clause at the end of the btn_Click declaration line. This clause automatically create a handler. If you want to have complete control on whether the event is activated or not, you should remove that Handles clause.

On other possibility is that the signature (the parameters) is not good for a Click event.

Putting these two possible problems together, your method declaration should look like the following:

Public Sub btn_Click(sender As Object, e As EventArgs) 'No Handles at the end

If this is not what you meant, then simply rephrase your question.
0
WatersideAuthor Commented:
Oh.  I just discovered that the handler can't 'see' btn_chick regardless of the state of hasHandler, so it's a scope issue ?

I can 'see' Public subs between class files but not those in code behind files.
0
käµfm³d 👽Commented:
You can only "see" them if you create an instance of the target class, or if you make them Shared methods. This is how all of .NET works. (Well, modules in VB.NET are a little different, but really they are just syntactic sugar for a Shared class.) However, since we are talking about event handlers, IMO it's a bad design to instantiate a form class just to gain access to one of its event handler methods. Instead, I would break out the common logic into a utility class. Then have each handler on each form call the method from the utility class.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WatersideAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.