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

I'm really not familiar with C #, and I'm sorry if this is a simple question, but it will help me better understand my homework.

I need to make some C # application in Visual Studio, and my main question is: what part of the code is in the file: form1.cs, form1.designer.cs or program.cs?

I think that Visual Studio generates code in Form1.designer.cs and that I should not change it, if it is not necessary, in form1.cs there is a function that is activated by clicking on the form element and in program.cs is the main application.

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

Thanks so much for your answers.

+7
c # winforms visual-studio-2012
source share
3 answers

Yes, you are mostly right, however:

  • form1.cs is the code for the window form file. This is a window class file. a form where necessary methods, functions, and event-driven codes and codes are recorded.

  • form1.designer.cs is a constructor file in which form elements are initialized. If any element is dragged and dropped in the form window, then this element will be automatically initialized in this class.

  • program.cs is the main application. This will be done the first time the application is launched.

+3
source share

Well,

  • form1.cs: Here you record your code, events, and custom code.

  • form1.designer.cs: Code for the components on the windows. You need it and you cannot delete. It is not recommended to change it manually for beginners.

  • program.cs: In C #, to start a program, it searches for a static class that contains a static method called main(string[] args) and starts executing the program in this area. Here, in a Windows Forms application, the code creates the form and opens it to the user using the application.

Each time you create a form, you will see that you have a .cs and .designer.cs , and every time you drag a control from the toolbar or change a property in the properties window, a .designer.cs file .designer.cs will be changed.

+2
source share

program.cs is a static class that contains only one static method needed to run your application. From MSDN :

Each C # application should contain one main method that determines where the program will start.

If your project is just a library, then you do not need Main() - the method in your code, and program.cs will not be created

About form1.cs and form1.designer.cs is one class form1 whose definition is split into two code files. From MSDN about partial class :

You can split the definition of a class or structure, interface, or method over two or more source files. Each source file contains a section for determining the type or method, and all parts are combined when compiling the application.

So these two files have code of the same class.
You can write control initialization code in form1.cs . But you need to remember that the form1.designer.cs file will always be generated when you make changes using the VisualStudio constructor

0
source share

All Articles