Visual Studio 2010 WinForms designer deletes code in unused event handlers - c#

My VS2010 WinForms designer keeps doing strange thing.
Let's say I have a button and there's a method/handler assigned to the button's Click event. This method contains some code. Now if I want to stop using the method or just to use it somewhere else, I select the button and open its events list, where I unbind the method from the event.
The result is that the method stays but the inner code is gone.
Why? If it's supposed to be some kind of optimization, then I'm not interested.

Related

Is there any way to NOT get a Designer error when deleting code? (C#, Visual Studio, WinForms)

I'm making a WinForms program for my Diploma final project using C# and Visual Studio.
Sometimes I accidentally double-click a tool and that opens up the main event for the tool, like button_Click. But when I delete that code and check the Designer, it gives me an error message. If I choose to ignore it, all of the Form's formatting is lost and I have to start over (real pain in the a**).
I usually just click the - to minimise the code block and then add comments that the code is unused. But as you can guess, this makes for a really ugly and unorganised coding page.
Is there any way for me to remove the code and not get a Designer error?
Go to the Form.Designer.cs file or F12 on the InitializeComponent() method.
Then on the right margin look for the red dots and delete the events pointed to methods that have been deleted.
Anyone got a quicker way?

How to Generate a C# Event Handler Stub from Visual Studio 2012 Source view?

Many times the signature of a control's event handler isn't obvious. Currently, in order to generate an event handler stub in the code behind I have to
Leave Source view and enter Design view (and wait for the UI to be generated)
Click on the control in question (which sometimes isn't obvious)
Open the Properties toolbar
Click on the lightning bolt
Double-click on the event for which I want to generate a stub
Is there an easier way to do this while still in Source view?

Delete control from form

I've currently got custom control that has somehow lost its parent and is now not parented to anything but it's still in the list of controls in the form designer. The delete button also doesn't work and is thus disabled. This happens every now and again and its a pain to go through the designer code and remove manually, plus there are other developers that this will annoy and may confuse them.
I'm therefore trying to add a Verb within the controls ComponentDesigner to delete itself from the form. But I realised that because its not 'childed' to anything, it therefore cant be removed as a child. How would I therefore go about deleting a control from the form designer via code?
This can happen when one of your controls throws an exception at design time. That's rarely a silent event, the designer shows a popup message box. Not getting a message box may happen when you swallow exceptions in your code with a try/catch.
Trying to fix this by hacking a designer just adds to the problem. Fix it by editing the designer code, it is okay when you know what you're doing. If you can't find the reason then get it to a point where you can make it somewhat reproducible. Then start another instance of Visual Studio, Tools + Attach to Process and select the first instance. Debug + Exceptions, tick the Thrown box for CLR exceptions so the debugger will stop when the exception is thrown.
Back up the file.
Open up the designer file; e.g., Form1.Designer.cs
Expand this region: Windows Form Designer generated code
You should be able to find your control in the code and delete it. Be careful.
I solved the issue by finding the loose controls within the Document Outline tab. This way is super easy and is graphical.

Moving asp.net team from VB.Net to C#, seems are we are missing something with wiring up events?

An asp.net (mostly webforms) team I work on has recently decided to make the move from vb.net to c#. The people who used to be on the team who couldn't read/write C# code well had since moved on, and each time we hired a new person it became more of a problem for us to be a vb.net shop.
I'll save the standard vb/c# language war stuff and just stick with the one thing that I ran into today.
With vb.net, when we want to handle some event from some object, we just pick the object out of the list in the upper left, pick the event out of the list on the upper right, and it creates the fully wired up event handler. It's also very clear that the method is an event handler due to the "Handles ... " at the end.
In the C# world, it seems we are left with a few poor options.
1) Manually wireup the event by typing the name of the event and event handler in the aspx markup. Like actually writing "OnLoad=SomeFunction" in the aspx. (EDIT: I didn't realize that his only applied to the form and not to any contained controls, so I guess this isn't really an option)
2) Using auto wireup events, and naming methods and signatures correctly so that they match up.
I'm hoping I'm missing some other much better option.
With these 2 options that I've seen so far:
1) we have to know the event names and event arguments. We can't just look down the list and be like "Oh good this has OnItemClick", I have to go research that in some control documentation. Likewise I'd have to go research the event args to find out that ItemClick needs a RadPanelBarEventArgs as the 2nd parameter. Wasted time.
2) I get no compile check on anything I'm doing with regard to connecting events. If I name the method signature wrong, or if I type it in wrong in the aspx page, everything will compile just fine until runtime when the mismatch will blow things up. (EDIT: what I mean is if I type onClick="ABC" and the event handler is really named "ABCD" this will not be caught at compile time. With VB, issues with renaming/mistyping event handler names are caught at compile time, because of the "Handles". Same goes for if I have onClick="ABC" and this controls onClick really needs a SpecialEventArgs but I accidentally use SomeRegularEventArgs)
I'm hoping we missed something and there is a better way to do this.
This is a known 'bug' or missing feature. See Visual studio 2010 showing available events from code behind
If you want to auto-generate events in C#:
Go to your aspx page in Design View.
Make sure the properties panel is visible
Either select a control with your mouse, or use the
dropdown under properties to select the control.
Move to the Events tab (the lightning bolt)
Find the event you want to wire up and either write in a name and hit Enter, leave it empty and hit Enter, or Double-click it.
It will create the event handler with all the
properties.
As for Number 2. Are you putting your code in a code-behind file? .aspx.cs?

How to help Intellisense so it lists event handlers in my aspx pages?

I've got a custom button control that handles an OnServerClick. My aspx dev's are asking me if it would be possible to influence Intellisense so it will give me all the possible event handlers for that page. Is this possible?
Basically I would like to have auto completion on my OnServerClick events in my ASPX pages (just like when I would have an enum).
Any idea?
It's not possible to get intellisense on server side events in the aspx.
It probably only works in the designer. When your dev's want to see the possible handlers, they can check out the designer and view the click eventhandlers in button properties / events / click.
Simply a limitation / bug / you name it of Visual Studio 2010 and lower.
But: maybe VS 2011 will make you happy! :
In VS 2011 Developer preview, markup intellisense for all ASP.NET
server-side events will show you list of exiting handlers and a value
called “Create New Event” . Which means you can attach the event
with some exiting event handler or select “create New event” to
generate an new handler with the right signature in the code-behind
file.
For more info and screenshots:
Server Side Event Handler Generation From ASP.NET Markup in Visual Studio 2011 Developer Preview

Categories

Resources