C# Designer: Removing redundant code from Design.cs - c#

I have a form which has several buttons, labels and other stuff on it, but the designer appears to remember old elements of that form that have previously be deleted.
Is there anyway to 'refresh' the form.Designer.cs code to inform it what elements are currently on the form and what elements are not.
Cheers for the help in advance ;)

If they are still in the designer file after a restart of VS and a re-open of the designer then they haven't been deleted yet. I've seen several cases where people think that variables are deleted but what's happen is they've been moved off of the visible area of a form. So they still exist but they're not doing anything interesting.
What you can do is close the designer, carefully delete all of the variables you want from the designer.cs file, recompile and re-open the designer. Before doing this I would backup the file though as it is possible to mess up your control by doing this.

Related

How to delete “empty code templates” in Visual Studio 2019

My professor took off points on an assignment for “leaving Empty Code Templates throughout my submission”. I’ve included a picture of what he was referring to. If anyone could help explain how to remove these?
I tried deleting them, hoping it would be all fine but I just get an error when I go back to my form.
As mentioned by Blue in a comment
Click on the associated controls in the designer view, and click the lightning bolt to view events. Delete them from there and you should be free to delete the leftover code.
It is a bad idea to edit event code signatures outside the designer.
Make sure you open the designer often or have frequent version control to avoid file corruption.
a friend just fixed it for me, she said: “ just delete what you want to delete and when u go back to ur program and it has that error click on “go to code” on the top right corner and delete the part that’s underlined in red”

C# Designer file: Error rendering control. Designer file out of sync

I have been looking at this for days. I have tried every option in the book and on the web, but have no luck.
There were no changes to the code where I am getting the error, but the designer file is out of sync. To me it seems like the NoteEditor.ascx.cs file can't read its control file NoteDetail.ascx.cs.
I am not sure why, I have tried deleting the designer file and recreating it. I tried closing all the project tabs and Visual Studio and reopened it, recreated the Designer files with no luck.
The error is that some controls don't have definitions even though they are all defined.
Is ChangeMode a control within the user control? Is it nested within another control?
When you look at designer.cs, does the control appear there?
If not then perhaps the designer isn't detecting your control. Try moving it to another location on the user control. Edit the ID, which may force the designer to recognize it. Once that happens you can move the control back to where it was.

Deleting an item off of Windows Forms in both the Code.cs and Code.cs[design] properly

When I'm making a Windows form application, and I use the toolbox and drag an item onto the form, I get a nice tool on the form, like button, listbox, etc. When I double click on the button, listBox, etc it goes to the text code part where all the delegates are. Also known as the event handlers. But what happens when I want to delete an item on the visual part of the application (the Code.cs[design] part)? I select it, then right click it and then delete it. It's off the screen. But, the problem is with all the event handlers. They're still left in the text-based code section but I don't need them there at all, and they generate a whole bunch of errors because references are gone, etc.
So, basically, I'm asking someone on the forum how to completely get rid of a button, listBox, both the visual part of it in code.cs[design] and in the code.cs part? I don't want to go line-by-line in deleting junk, I want to get rid of the button/listBox and it's accompanying code in one fell swoop.
It will delete such an automatically added event handler. But only if you didn't modify it and left it untouched with no code added. Visual Studio will not delete code that you have written. The reason for that should be obvious.
If this happens a lot then you need to get a pair of scissors and cut the mouse umbilical cord. Design first, program later.
I can't be done and it shouldn't.
You first delete the visible control and after that you delete the code stubs.
The handler assignments in the Designer.cs are then being deleted with the visible control automagically, as they were created.
Just make sure to get the order right: first delete the controls then the code.
Unless you mix those up there is no need to ever edit the designer.cs file. The code may contain important stuff you wrote. Therefore is is not deleted by studio.
You create it, you delete it! Two steps, period.
PS: The code stubs are (unfortunately) added chronologically. It would be nice if they were grouped by the controls they belong to. If you are in doubt, you can always use the 'find all references' command to find those methods buried in the past..
If you have a code stub generated, delete the method. Once that's done, Click on your 'forms view' - you will have an error screen shown.
Click on the link, and it will being you to the designer view, with your cursor shown on a specific line. Delete this line of code. Having that done, you could then delete the physical element from your designer.

Controls on WinForm not modifiable in Designer

I've inherited a project that has been modified in a way that is beyond me to undo. Every single control on the form is not modifiable in the designer except through the Properties window and each control has an icon in the area below (see image) in the section usually reserved for non-visible items (e.g. DataSource). How do I undo this and return the designer to a usable version without wiping it all out and starting over?
Could the controls be locked? I have run into legacy code in which the programmers have locked every single control, meaning you cannot move or resize them in the designer. If so, select them all and change the Locked property to false (you should also see a padlock in the designer when you select them, if they are locked.
If not, well maybe you can create a new dialog/form and copy the controls unto it and see if that clears things up. Another trick could be to add a second instance of the user control(s) to see if the newly added control works as expected. If so, go through the code and point all events/logic to the new control (tedious yes, but may work).
Also, you can never edit individual sub-controls of a user control in the designer for the form/control where you instantiated the user control. You must go to the designer of the user control to edit individual (sub)controls of a user control.
If the Infragistics controls are shown in the Component Tray, than there might be version differences. What you could try is to open the licenses.licx file and remove the content from there. Do you have an Infragistics controls installed on your machine? Are there any difference after setting the "Specific Version" property of the Infragistics assemblies to "false"?
Is this happening only in your existing project or the same behavior is reproducible in a new project as well?
You have to load the dll that contains the controls you can't modify in the designer.
the steps are:
right click in the ToolBox in visual studio designer (on General for example).
select Choose Items...
after a while vs load items press browse and select the dll
interested than the componet should appear in the grid. Do this for
all the dll that contains the controls you are interested.
-Make sure you have checked the component (use filter text box if you
have a lot of component loaded).
press ok.
Now you should be able to move controls in designer.
I have also seen behavior this when a user control or form is incorrectly flagged as a 'Component'.
Some manual hacking of the .csproj file may be the answer then. Open the project file in a text editor, and find the references to your control. If you find a subtype defined as:
<Compile Include="MyControl.cs">
<SubType>Component</SubType>
</Compile>
The forms designer will interpret it as such. You can change the 'SubType' to 'UserControl' to fix it.

Visual C#, Winforms, and Partial Class Madness

I haven't done much work with .NET, so forgive me if this has a trivial solution.
The "problem" (more of an annoyance, really) is that the VC# IDE opens all files that have a class which inherits from System.Windows.Forms.Form in design-view, by default. The only exception being "*.Designer.cs" files.
Generally speaking, this is fine. However, if I have a partial class that implements some control handler code, this becomes quite an annoyance. When double-clicking on the source file, for instance, you are taken to the Winforms designer and presented with an empty form. Totally useless.
To get around this, you are required to right-click on the source file in question, and select "View Code" from the context menu.
My question is rather simple: is there any frigging way to get around this absurdity?
If not, I'm wondering how people organize their UI-handling code.
Thanks!
Yes.
In the file's context menu, select "Open With..." and use the "Set as Default" button with your prefered editor selected (in your case, probably the CSharp Editor).
Rather than right-click and view-code, I always use Doubleclick and it goes to the code page, focussed on the Form_load event, which can be left blank and ignored.
If you really want to fix the behavior, I suggest somehow making your file not be a Form! Cant blame VS for thinking you want to work with a form when you open a form's file!
Single click the file in the Solution Explorer and press F-7.

Categories