Solved

Having issues with RemoveHandler for a User Control?

Posted on 2015-02-16
4
233 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

615 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