VS2022 Community: GUI keeps creating new Click event for my buttons

SteveInReno

New member
Joined
Mar 13, 2023
Messages
2
Programming Experience
10+
Building code behind a simple button; randomly when I dbl-click the button in the IDE it suddenly opens a NEW Click event (original = myButton.Click(), suddenly a myButton.Click_1() is created) and the code in the original event no longer runs. I have to create a whole new button, copy the code into it, and move on until it breaks again. Deleting the magically created new events don't fix the problem; seems behind the scenes my button, named "MyButton", is no longer "attached" to the code behind it.

What the hell? Older versions of VS don't do this; do I need to keep using 2018 or 2017 to get my work done? This is rediculous.

Thanks.
SteveInReno
 
Can't see anything wrong with the IDE, but if you happen to Cut a control and Paste it elsewhere in form, for example into a different container, then the Cut operation will detach the event handler from the control, while the handler Sub will still be there in code. If you then doubleclick to get to event handler it will create a new one, for example if you had:
VB.NET:
Button1_Click(sender, e) Handles Button1.Click
after Cut code looks like this:
VB.NET:
Button1_Click(sender, e)
If you then doubleclick to get to/create event handler the result is:
VB.NET:
Button1_Click_1(sender, e) Handles Button1.Click
because the method name "Button1_Click" already existed in code.

If this is the cause then use drag-drop instead, or the Document Outline window, to move the control and not Cut it.

The alternative if you have orphaned the handler methods is to select the control in designer, select Events view in Properties window, and for the event(s) in question select an existing handler method rather than creating a new one.
1678741286724.png
 
There is a known issue in VS 2022 where Handles clauses are being removed from event handlers. If that happened to you then double-clicking a control will, indeed, generate a new event handler. I'm not sure of the specific circumstances required to recreate the issue - I haven't seen it - but it will remove all Handles clauses in a code file. There is a fix in the works apparently but you just need to be on your guard for the time being. If it happens, you can revert using source control - you should already be using source control and, if you're not, start - or you can add them back manually or you can use the Properties window to select existing methods for events.
 
Found this bug report: Visual Studio Feedback
I was easily able to reproduce the problem with a .Net 7 winforms project, that's a horrible bug! Basically about any change in designer when code file is also open cause all Handles to be removed.
They say 17.6 is close to release and will fix it, and 17.4 was good if rollback is a possibility.
 
Back
Top