Solved

Having issues with RemoveHandler for a User Control?

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 70
Help with Syntax 9 36
Pagebreak issue while printing the aspx page 3 24
vb.net class 3 12
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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