Solved

Having issues with RemoveHandler for a User Control?

Posted on 2015-02-16
4
231 Views
Last Modified: 2015-02-17
I have a User Control that contains two combobox controls. Each of these comboboxes has a SelectedIndexChanged Event defined. I don't wish to have these events executed when the Windows Form loads so I'm trying to use the RemoveHandler/AddHandler statements. However, in the definition of the RemoveHandler, I'm getting a design-time error. Please see screenshot.
screenshot.jpg
0
Comment
Question by:BlakeMcKenna
[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
  • 2
4 Comments
 
LVL 40
ID: 40613107
When using the code editor or the Form Designer to create an event method, as a security as well as a good design feature, event methods are declared Private by default. They can thus be manipulated only from inside the in which they are created them.

If the code in which you use Add/RemoveHandler is in the same project, change the Private modifier in front of the event declaration to Friend. That way it can be called from anywhere inside the same project.

If the code is not in the same project, then set the modifier as Public, which enables any project that has a reference to deal with the event method.
0
 

Author Comment

by:BlakeMcKenna
ID: 40613144
"If the code in which you use Add/RemoveHandler is in the same project, change the Private modifier in front of the event declaration to Friend. That way it can be called from anywhere inside the same project."

The Add/RemoveHandler statements are in the code-behind of the Form that the UC resides on. I'm assuming the Event to declare as Friend is in the UC?
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40613363
Normally, if the control, its event procedure and the Add/RemoveHandler are in the same class/form/code file, Private should be OK and you should not have that error message.

And looking closer, something puzzles me in your .jpg.

Your are trying to remove the handler on usrChannelDirectionLoad.cmbDirection1, but the error message states userChannelDirection, user instead of usr at the beginning and no Load at the end. It is as if the compiler was confused there, giving you an error for something different than what you called for.

Do you have something called userChannelDirection in your environment. If so, what is it and what is its relation with usrChannelDirectionLoad.

Or do something that will enable you to skip that code completely.

I see that you are calling RemoveHandler in Form_Load. This is not necessary. Simply removes the Handles clause at the end of the cmbDirection1_SelectedIndexChanged event declaration. What that clause does is to instruct the compiler to issue a AddHandler command so that the event is active by default. If you remove the Handles, the event is seen as a normal Sub and is not active when you load the form. Simply call AddHandler once you are ready to activate it. There is only one drawback to removing the Handles clause. It is that the environment does not see your method as an event. It thus won't be seen in bold in the list of events for the control, but will show instead as a normal Sub or Function in the list of methods of the Form.
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40614242
Hi James,

You actually triggered some thoughts and I was able to figure it out. I see what your saying about the .jpg. I looked into that and played around with it as well. I finally got it working!

Thanks!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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