Role of Designer.cs File in c# - c#

I am getting Designer.cs file in my project and the comment in the file says it is being generated by an automatic tool.
This was an existing project so I don't know much about that. It is generated for one schema.cs which consists of schemas of all the tables in Database.
I am using a SQLIte DB.
Can any one help me in understanding what is the use of the Designer.cs file in a C# project.

There are lots of kinds of Designer.cs files in a Visual Studio project. Common ones are:
Properties\Resources.resx\Resources.Designer.cs file, the auto-generated code from adding resources to the Project + Resources tab
Properties\Settings.settings\Settings.Designer.cs file, the auto-generated code from adding settings to the Project + Settings tab
SomeForm.cs\SomeForm.Designer.cs, the auto-generated code produced by the Winforms designer from the controls you drop on a form
SomeData.xsd\SomeData.Designer.cs, the auto-generated code produced by the dataset designer.
Given that you name a database in your question it is somewhat likely that you are talking about the last one. You ought to see the pattern from the descriptions, you use a visual designer gadget in Visual Studio and it produces C# code that does something useful at runtime. You go back from the Designer.cs to the designer gadget by double-clicking its parent node in the Solution Explorer window. Don't ever edit the Designer.cs file, you'll lose everything when the designer re-generates the code. They are somewhat hidden in the Solution Explorer for that reason. If you haven't found them yet: open the nodes to see them.

Designer.cs contains the declaration and initialization of UI controls and layout of form. The form is rendered based on the information provided in designer.cs. This file is autogenerate when a form is created in design mode.

Related

Disable Design View for C# Class in VS2019

My C# project displays classes in the Solution Explorer some with the Designer View and some with the normal code editor:
AND
I Would like to make them all open with the code editor!
I went in the .csproj file and I noticed that for the classes marked as designer, I get a
<SubType>Component</SubType> tag. If I remove it manually, VS automatically re-add it to the item.
How to solve it? I searched but did not found anything usefull..
EDIT 1: I forgot to mention that this is a Console Application

How to rebuild WinForm with just Designer file

I recently picked up a project of mine from a few months ago that I had stored on Github. However, there are a number of forms that apparently did not get stored in the repo. I'm not sure how that happened, but I'm trying to recover as much as I can.
For example:
- CreditsBox.cs
- CreditsBox.Designer.cs
In this case, the CreditsBox.Designer.cs file exists, but the CreditsBox class file does not. Fortunately, the forms that this happened to didn't have too much login in them, so its trivial to rewrite. However, it would make it 10x easier if I could somehow rebuild the form with just the designer file?
Any help is appreciated!
The designer file is all you need to re-create the UI of your forms.
You could follow these steps
Create a new Project.
Add a form and name its file as CreditBox.cs.
Use your saved designer file to replace the one created by Visual
Studio.
As far as I know, the last step could be done also with Visual Studio open but, in any case, better close VS and reopen it afterward.
Of course you could do the same for all other form designer files with the missing main cs file.
So I found a somewhat easy way to fix this.
Create a backup of the .Designer.cs file and delete the original from the project. Then, create a new form in your project with the original form's name (so dependent methods/calls don't fail). Then go through your original .Designer.cs file and create the objects simply by dragging from the Toolbox onto the form and naming it to match the original file. Once you have all of them added (don't worry about styling), copy the old Designer.cs content back in so it will fix the styling, spacing, and all that. Once you build, the designer will update to the original styling/spacing/etc.

What kind of .cs extention file include both design and code? C#

I have 2 question.
1.When I create new form with Visual studio then Form design and code are separate files.
Form1.cs
Form1.Designer.cs
Form1.resx
But I downloaded "sample_client.cs" from internet which has both design and code in same "sample_client.cs" file.Why is that? Was it made by Visual Studio or not?
Why some Windows form source is XAML?
Is there something special about it?
One question a time:
Form1.cs and Form1.Designer.cs contain the same class as Partial Class. The second is updated by the IDE itself, the first one by the developer. The example you downloaded probably comes from an other development environments. To discover which is, you have to open the project file and dig into it...
The XAML files belong to the Windows Presentation Foundation (WPF) technology, the example you provided with ".cs .resx" file belongs to Windows Forms technology.
There is a language feature in C# that allows classes to be split into multiple files by using the "partial" keyword.
This is mostly used for splitting a class in an auto-generated part and a user defined part. It can be a good thing to separate user code from the auto-generated to avoid custom code to get overwritten by the generator. This is why the Visual Studio designer splits the same Forms class in a .designer.cs part and a .cs part.
The .designer.cs file is really just plain code for initializing the Form and since it is the same class it can be merged into one file, which is probably what someone has done here.

Any way to make Intellisense work, when opening a cs file that's not part of the project?

What we ideally need is, to know how Microsoft handles XAML generated code (Those *.g.cs files). If you goto a XAML code behind, intellisense will work even if the *.g.cs file is not part of the project!!
Context:
In a custom VS package, we need to have some logic to open a CS file (with out making it a part of the project) in the VS IDE, to allow the user to edit it.
We are hooking up the document to the Running document table and receiving the events like Saving, Close and all, using IVSRunningDocumentTable. Works good.
Now the problem is, when the document is opened, Intellisense can't work, for the simple reason that the opened document is not part of the project (sadly, we can't do that, we can't make it code behind).
Intellisense is driven by a memory cache of identifiers and types. These types are cached based on the project you are in and the references that project has. If the code file you are editing is not part of a project, Visual Studio would have to load every possible assembly and create intellisense data for each type in the entire .NET framework because it would have no way of knowing whether or not your code file required it.
I guess Visual Studio could load intellisense based on the content of the file but that is not the way it currently works.
Visual Assist X by Whole Tomato is an addin to VS I've been using for many years. It will give you Intellisense and more when you open it.

What could cause Visual Studio / C# error MSB3105: Duplicate resources

While working on an existing project I suddenly got the following error when trying to compile the solution:
error MSB3105: The item "[filename]" was specified more than once in the "Resources" parameter. Duplicate items are not supported by the "Resources" parameter.
Now, as far as I'm aware, I did not make any change to the project that affects the resources. Also I have checked each and every file within the project, but there is no duplicate reference anywhere to this file.
Now I already found some forum entries regarding this error:
1) Open the .csproj file and remove the duplicate reference. [Tried this, but I cannot find any duplicates in it]
2) In a 'partial class' project, move everything to a single class. [ Could try this, but the project has been split up into partial classes since the start, and I do not want to change this just because of the error ]
So what else could cause this ?
Did you try showing all files in the Solution Explorer? You could have a duplicate .rsx file somewhere in there.
I found the answer in .NET forum posting by Roy Green, and Theresa was right after all, though I did not recognize it.
If you have your main form class split up into partial classes, the partial sections end up in the solution explorer as separate items. And if you double click on them they show up in the designer mode as a normal form. But if you (accidentally) drop a control on these forms, Visual Studio creates a new .resx file and a InitializeComponent routine for it. But since this form is actually just part of the Main Form class it leads to the 'duplicate resources' error. And there is no other solution but to remove the InitializeComponent routine and delete the .resx file by hand.
Be sure that under yourForm.cs no duplicate resources are defined (.resx). If you renamed your Form, remove the old resource because the new one during compile will be created with the new name.
I just made the same mistake. Delete the mainform.designer.vb, then I restored it again from the recycle bin, and found this error message when compiling.
I try to search on google and someone suggested to check on .vbproj. Did that and found a duplicate on some line.
I had this as well, in VB. There is the "real form" file frmMain, and then I had created new class files and modified them to be Partial Public Class frmMain. For example, I have an ImportFromExcel.vb Partial Class file (I didn't want to clutter up the frmMain.vb with the rather complicated Excel import code.)
Everything worked fine until I decided I wanted to use an OpenFileDialog in the Sub ImportFromExcel. I dragged the OFD from the toolbox over to the Designer view of the ImportFromExcel file. (I have no idea why this view exists, if you can't do anything with it!) But at any rate... dragging the OFD to the Partial Class Designer created an ImportFromExcel.resx file. The drag/drop operation also created an InitializeComponent sub in ImportFromExcel, which is redundant and shows an error -- easily corrected with a little editing.
Ultimately, I chose to not use the dragged resource, but localized the code in the ImportFromExcel.vb file.
All you really have to do is right-click the ImportFromExcel.resx file, and choose Delete. Everything else seems to "fix itself", and it builds fine now.
In my case, this problem happened because a file had the same name but not the same case in the GIT repository.
For example MyFile.cs and myFile.cs.
If you do a checkout on windows, one of the files is overwritten by the other (no message, no warning). So, it is compiling, and we don't notice anything. But if you try to compile on Linux (with .NET Core) both files are present, and there is this error at compile time.

Categories