What kind of .cs extention file include both design and code? C# - 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.

Related

opening c# files with visual studio outside of project gives no suggestions

Im currently writing some very small c# exercises, for a algorithm course.
visual studio is my favourite IDE, and usually, when i create or clone a visual studio project, I get the full functionality of viusal studio including spellchecking and suggestions for fields and methods on objects and so on.
But right now I am just trying to open a single .csc file and write some code in it. The problem is that when i do that, i get no suggestions. So if I create a list I would usually be able to view all the methods and fields inside the list class simply be referencing an object. Syntaxm checking works fine.
How do I turn on intellisense suggestions in a file that is not in a project?
Thank you
The simplest way is simply to add that file to a project.
Open VisualStudio.
Create a new project (you can probably use Console project or Class library, depending on what you're doing).
Add your file in that project.
Make sure your file as the Build Action C# Compiler.
And that should work.

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.

why do I need Default.aspx.designer.cs file?

when I used to create ASP.NET website there was only one code behind file as for example Default.aspx.cs. Meanwhile I created ASP.NET web application, this time visual studio generated an additional code behind file as Default.aspx.designer.cs My question why do I need it ? I inherited a class other than the Page class, so I removed "inherits="MySite.Default". But I've got an error mentioning identifier expected in the Default.aspx.designer.cs file. Please help me in this regard too.
the designer files are auto-generated by visual studio and act like scaffolding for the code you write within the aspx and aspx.cs files. Because these are auto-generated you never need to or should touch these files.
I would advise you put the code back where it was, inherit from your desired other class as you wish and let visual studio worry about the designer file.
Simply make the changes your need to those the aspx and aspx.cs files and forget about the designer files.
Find more info here: aspx.designer.cs how does it work?

Role of Designer.cs File in 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.

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.

Categories