Solved

Having issues with RemoveHandler for a User Control?

Posted on 2015-02-16
4
223 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
  • 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now