Tutorial :Handles keyword dissapears on control cut/paste


When I cut some controls in designer window and paste them in some other container, handles keywoards dissapears from code.

If I have btnOK on form and click event handled like this:

Private Sub btnOK_Click(...) Handles btnOK.Click  

and then in designer I cut the button and paste it somewhere else, Handles part just disappears, living the method.

I understand why this happens, cutting like this is like deleting the control but silently removing parts of the code is not nice behavior from editor.

Is there any workaround, preference or plugin to add some warning before messing with my code? Or even better, to keep handlers and leave removing to me?


The best method I found was to not cut/paste at all in DotNet. You should be able to move your controls in and out of other controls without the need to cut/paste. Bring to Front and Send to Back also come in handy.


When you remove a control from your form, the IDE automatically removes all the handles from your code and leaves your code intact:

// Previously:  Private Sub btnOK_Click(...) Handles btnOK.Click        MessageBox.Show("hello world")    End Sub    // Now:  Private Sub btnOK_Click(...)        MessageBox.Show("hello world")    End Sub  

When you paste your control and (in the case of the button) double click it to attach some code, the IDE detects that there's already a function called btnOK_Click and it creates a new function for you, named btnOK_Click_1. This is a behavior by design which you cannot change.

What you can do is to paste your control and then go to the properties window, switch to the events, find the Click event and use the dropdown to select the original function.

Alternatively, you can just go to your code and add the keyword Handles btnOK.Click at the end of the original function.


For prosperity I'd like to add a comment to this issue. I often have a need to replace buttons in my systems. This nagging 'feature' always increases my workload. I minimize the affect by going to the code and changing all Handles occurrences with 'Handles thus commenting out this statement. I cut the old buttons, paste the new buttons then uncomment the Handles statement. It's a quick way around the issue and allows me to rapidly make my changes. I hope this helps.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »