Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between form1.cs, form1.designer.cs and program.cs in c#

I'm really unexperienced with c# and I'm sorry if this is to easy question, but it will help me to understand my homework better.

I have to make some kind of c# application in Visual studio, and my main question is: which part of code is situated in which file: form1.cs, form1.designer.cs or program.cs?

I think that Visual studio generates code in Form1.designer.cs and that I shouldn't change it unless it is neccessery, in form1.cs are function that are activated by click on some form element, and in program.cs is the main of the application.

Am I right, and is there anything else that I should know about these files at the beginning?

Thank you very much for your answers.

like image 249
user3100193 Avatar asked Jan 08 '14 17:01

user3100193


People also ask

What is form1 designer CS?

form1. cs is the code-behind file of the windows form. It is the class file of the windows form where the necessary methods, functions also event driven methods and codes are written. form1. designer.

What is form1 Resx?

form1. resx is most commonly used for storing strings that you want to translate into different languages so you don't hardcode them into your app.


3 Answers

Well,

  • form1.cs: It is your code, events and custom code you write are here.

  • form1.designer.cs: The code for the components on the windows forms. You need it and you cannot remove. It is not recommend to change it manually for begginners.

  • program.cs: In C#, to a program start it looks for a static class that contains a static method called main(string[] args) and start executing the program in this scope. Here, in a windows forms application, the code creates an form and open it to the user start using the application.

Everytime you create a form, you will see you have the .cs file and .designer.cs and everytime you drag a control from Toolbox or change some property on the Property Window, the .designer.cs file will be changed.

like image 140
Felipe Oriani Avatar answered Oct 22 '22 09:10

Felipe Oriani


Yes you are mostly correct, however:

  1. form1.cs is the code-behind file of the windows form. It is the class file of the windows form where the necessary methods, functions also event driven methods and codes are written.

  2. form1.designer.cs is the designer file where form elements are initialized. If any element is dragged and dropped in the form window then that element will be automatically initialized in this class.

  3. program.cs is the main of the application. This will be executed first when the application runs.

like image 33
Rashedul.Rubel Avatar answered Oct 22 '22 10:10

Rashedul.Rubel


program.cs - is static class which contain only one static method which need for starting your application. From MSDN:

Every C# application must contain a single Main method specifying where program execution is to begin.

If your project are just a library, then you don't need Main() - method in your code, and program.cs will not be generated

About form1.cs and form1.designer.cs - this is one class form1 which definition are splited in two files of code. From MSDN about partial class:

It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.

So this two files have a code of same class.
You can write code of control's initialization in your form1.cs. But need to remember that form1.designer.cs file will be generated always when you made a changes through designer of VisualStudio

like image 35
Fabio Avatar answered Oct 22 '22 09:10

Fabio