?
Solved

Having issues with RemoveHandler for a User Control?

Posted on 2015-02-16
4
Medium Priority
?
236 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 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

719 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