Delete control from form - c#

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.

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?

Winforms designer misbehaving, essentially broken

We have a complicated legacy form built in Winforms that recently has started seriously misbehaving and it gets worse and worse as time goes on. Just opening the form in design view will alter the layout of the form, sometimes in minor ways (a control is resized to be a pixel smaller) and sometimes in major ways (a component is moved to X position -6582). Due to the size of this form and the nature of the generated .Designer.cs referring to the diff is almost usually useless to determine whether the changes that were made to the form were the changes that you actually intended.
Based off a number of suggestions I found in other questions:
visual studio 2005 designer moves controls and resizes Form
Visual Studio keeps resizing my form without me telling it to, help!
Controls moving in vs2008 design mode on build
AutoScaleMode is set to None. I experimented briefly with various monitor resolutions, to no avail. Ultimately we decided to stop relying on anchors and instead use a combination of TableLayoutPanels and Dock properties. And that's when the trouble really started...
After heavily redesigning one of the hairier tabs on this form, when I tried to save Visual Studio spun for 2-3 minutes and reported "Could not load file or assmbly 'MyProject, Verion=X.X.X.X, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified" (as seen here: Visual Studio 2013 Can't save, build, or rebuild solution). This happened whenever the form attempted to save. After following the advice in the accepted answer, I was able to save/build again.
Attempting to run not much later resulted in a very, very long build time and a completely empty form (as seen here: All controls on a form are invisible and https://stackoverflow.com/a/5524183/1015495 ). All calls to Controls.Add had been removed from the designer (I assume this massive change is what resulted in the long build time). I restored the form to a previous version and began recreating my changes. Now, much more insidiously, certain controls have had all of their children removed.
Working within this form is becoming a timesink at best and unfeasible at worst. The solutions that I've found online basically boil down to "recreate the form manually" or "don't use Winforms". Unfortunately neither of these are an option right now. How can we mitigate the problems that we're having with this form in the mid-term future until we can come up with a proper fix?
I am working through an almost identical problem at the moment and mine seems to be a bug in Visual Studio 2013 related to having a "linkLabel" control on a User Control, which is on one of the tabs of a tab control on the main form.
I had other linkLabel controls on the parent form that did not cause any issues. The weird thing is that not ALL of the tabs were messed up, only some of them. I can only conclude that it gets through part of the layout restructuring, hits the user control, and bombs out, failing to finish the layout correctly. I attached a second version of visual studio to try to catch exceptions, and saw various status lines showing up in the debugger, but did not see any exceptions thrown. I am not that familiar with doing that, so it's possible I did that wrong though.
I could revert the code back to after I had added the user controls, but before VisualStudio opened the form in the designer and automatically re-generated the designer code. At this point, opening the form in the designer will always cause exactly the same design failures. As long as I never opened the code in the designer again, everything would work correctly at run time.
If I removed any reference to the custom user controls with link labels, the designer would also open without any trouble. At first I assumed I had an error in my own code so I started commenting things out, but it was still happening even with just the basic elements dragged on. So then I started removing elements and adding them back one at a time to see what broke it. In my case this turned out to be the "linkLabel" control. I am not sure if there are other controls that will do this yet. For now I just plan on re-writing the code to not use a link label on the user control though and see how well that goes.

Visual studio shows endless messages "Code generation for property 'valueMember' failed."

After several days of happily hacking away on this C# app using Visual Studio 2008, I get struck by a barrage of error dialogs showing:
Code generation for property 'valueMember' failed.
Error was: 'Object reference not set to an instance of an object.'
This happens now often when I make a tiny change in the designer, e.g. shift a control a few pixels, and then try to save. Several such error dialogs appear each second, keeping me busy cancelling all those by hammering the Enter key while trying to get alt-F4 to get VS to close.
Eventually I do get VS to close and to save the changes I made. After restarting VS, I do "clean" on the entire project, then "build" and everything works fine, the app runs fine, no problems.
Until I make another slight change in the form designer.
I don't know about any property valueMember in my app.
This makes me crazy, it is a real showstopper for my project. Any help is appreciated.
Try to Close and reopen the Visual Studio. maybe it seem silly, but it works!!
You can debug the designer using another visual studio and attach to process. If you got exception it should be easy to find it that way.
In general when openning the designer the constructor and of course initializeComponent is running.
As this is happening at design time, it is likely that you have a custom control which requires a parameter or other value which does not have a default.
When in design view in Visual Studio; a control instance is created to render it on the visual editor, but if the control requires a property to be set before it can be rendered, it will result in an error.
Can you check that all custom controls have default values, and anything referenced in the constructor that cannot have a default is wrapped by DesignMode property - see http://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode.aspx.
Similiar to #Chanipoz's answer (close/re-open) my component-rich/user-controls-everywhere forms app started to compile happily after I closed down the main form designer window.
I've had this code stack for years and have never seen the error until today. Not sure where it's coming from. But, something today about having the form open in the designer made everything unhappy. Simply closing it off of the screen made it all go smooth.
Use another instance of Visual Studio to attach to the first instance of visual studio.
Go to Debug-> Attach To Process and look for the devenv.exe process. Since you'll have two devenv.exe processes running you'll probably want to pick the one with the lower ID, that's usually the first instance of visual studio that was run.
I had to face this problem. As I have found the solution below
I am facing this issue in my customized control.
we need to implement like this
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public MyCustomclass _Prperty { get; set; }
I had to face this problem. As I have not found the solution (much inheritance), I can tell:
.SuspendLayout() and .ResumeLayout() may be missing in code or one of them. The same is with .BeginInit() and .EndInit(). It is expected between them, that there will be = new ... and some settings for properties. Maybe someone facing this problem would find the solution with this information.
The problem is missing initialization code for a public property on the control. This will be added for you when you add the control to the designer, but if you replace a control with a derived control, or update the component, then the designer does not know how to deal with this.
If you have a control (wincontrol) with a public property PropertyA, and you add it to a form (myForm), then the designer will add all the necessary initialization for properties into myForm.Designer.cs. Something like;
Wincontrol1.PropertyA = new List<widget>();
It is not uncommon to need to modify a control slightly, lets say we have a new control MyWinControl
public partial class MyWinControl : WinControl
{
public List<wodget> PropertyDer1;
protected List<wodget> PropertyDer2;
}
If you sub this new control for the old control in myForm.Designer.cs, then you may well encounter this issue. The reason is that PropertyDer1 has no initialization in the winforms designer. PropertyDer2 won't cause any issues because it is protected. Similarly if you had a custom component and you add a new public property after the component has been added to a form.
If however, you deleted the instance of WinControl on the form, and dragged an instance of the MyWinControl onto the form instead, the proper initialization would occur and you would not see the error. The designer will have created the new control like this
Wincontrol1.PropertyA = new List<widget>();
Wincontrol1.PropertyDer1= new List<wodget>();
There are two easy solutions that do not require hiding the property from the designer.
1. If the property doesn't need to be public, give it the right modifier
2. If the property does need to be public, then just edit the code in the myForm.Designer.cs as in the code above to add the missing initializer
If could be of help I just detected a case that brings that same error message, impossible to take away :
I am developing an application in French, and I had to create a ToolStripMenuItem with an accented word in it like "annulées".
The system generated a menu item like "annuléesToolStripMenuItem" and the accent is the culprit.
Enough to delete the item, create it again in English and the just change the Text property of the menu item.
Hope it will be of some help.

Winforms usercontrol phenomenon : suddenly all items are away!

I have a winform usercontrol with several items (textboxes, buttons,...). Now I am confronted with phenomenon, that all items are suddenly away (in the VS 2010 designer view / document outline), although in the control designer file (designer.cs) the textboxes, buttons,... are still defined. There is also no compilation error. When I run the application, the items are still missing! I have already restarted VS!
Does anybody know, what the reason for this is?
In the InitializeComponent method do all the controls get added to the form via this.Controls.Add... statements?
I know this happened with VS2003 a lot that these code lines would disappear. I suspect that this is the problem
Did you possibly remove the call to InitializeComponent from your control's constructor by accident?
Does the name of the partial class in your .designer.cs file match that of your "main" control file?
Try running your application through VS's debugger. Put a breakpoint at InitializeComponent and step through all the code responsible for creating and placing all the controls in your user control. Maybe you will happen upon an explanation this way.
Try to restart your Visual Studio, and the do a Rebuild Solution.

How to temporarily disable Visual studio auto generated events?

All,
I have finished the GUI design phase... Now I've started to add meaningful names to all the controls in my application. Visual Studio is driving me nuts auto generating the events each time I click on the control to change its name (ok so it only happens when I mess up and double click... but still annoying).
Is there a way to temporarily disable this feature? I still want it because it is a great short cut when I'm coding.
Thanks!
You can't disable double clicking AFAIK but you can open document outline (Menu View > Other Windows > Document Outline), and rename the controls via F2 in this window. It'll be much faster and will help see easily the visual tree if your form is complex.
afaik there is not. Just be careful with your clicking and if you do mess up CTRL+Z CTRL+Z is your friend :)
If you can recognize which controls are which in code, you could open the .Designer.cs files and rename the controls there with the refactoring tool (right click the control name, Refactor, Rename).
It may or may not be straightforward to recognize which controls are which given that you have used generic names so far, but at least it's an option. Personally, I avoid that situation by immediately assigning meaningful names.
It's not just renaming controls, it's resizing them by dragging that is a bigger problem. CTRL+Z works to remove the auto-generated code but simultaneously undoes the resizing of the control. Very annoying.

Categories

Resources