views:

19

answers:

1

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?

A: 

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.

Anax