locked
Newbie: Difference between Form1.cs, Program.cs ... RRS feed

  • Question


  • Hi

    I just starting and found the following files:
    - Form1.cs,
    - Program.cs
    - Form1.Designer.cs

    I used Visual studio and I only see the *.c file. I am wondering how come there are 3 different files and wonder about their purpose?

    Thanks
    Saturday, April 21, 2007 12:34 AM

Answers

  • The purpose for the three files is to hide complexity and improve productivity.

     

    The starting point for your program (the Main() ) is in Program.cs.  It sets some environment parameters and calls for the creation of Form1.  The code for Form1 is divided between the Form1.cs file and the Form1.Designer.cs file.  (I believe this division is new for this version of VS.)  Essentially, Form1.Designer.cs contains all of the code about the form that is automatically generated for you when you drag components to the form from the toolbox.  Form1.cs is where you write your code related to the form.

     

    In general, you should only write code in Form1.cs.  Let the IDE write the code in Form1.Designer.cs.  There may be some rare occasions in which you would want to touch Program.cs.

     

    Saturday, April 21, 2007 3:14 AM
  • Hi, 2006

    Generally, we put the application entry-- main method into the program.cs and put the logical code in Form1.cs while UI designing code in Form1.Designer.cs. In this way the application is tidy and easy to maintain and reuse.

    Imagine that all the code mixed up in a single file, it would be messy.

    In C# 2.0, partial class is added in. Then we can put single class definition into two or more files to seperate the logic and designer code. It quite make sense, right? In 1.1 or ealier one class must be define in one file, so it looks more messy.

    Of course, you can change the file name to whatever you want but with suffix .cs even not equal to the class name, it's ok. (sometimes we can define multiple classes in single file)

    If you have further questions, please feel free to let me know.

    Thanks

    Monday, April 23, 2007 5:24 AM

All replies

  • The purpose for the three files is to hide complexity and improve productivity.

     

    The starting point for your program (the Main() ) is in Program.cs.  It sets some environment parameters and calls for the creation of Form1.  The code for Form1 is divided between the Form1.cs file and the Form1.Designer.cs file.  (I believe this division is new for this version of VS.)  Essentially, Form1.Designer.cs contains all of the code about the form that is automatically generated for you when you drag components to the form from the toolbox.  Form1.cs is where you write your code related to the form.

     

    In general, you should only write code in Form1.cs.  Let the IDE write the code in Form1.Designer.cs.  There may be some rare occasions in which you would want to touch Program.cs.

     

    Saturday, April 21, 2007 3:14 AM

  • So what is really "Form" ?

    Is this equivalent of the "console" - or in the absence of the console, Form 1 is there.
    Saturday, April 21, 2007 6:20 PM
  • Form is another word for your standard "window".  The thing with the blue bar on top with the title on the upper left, the minimize, maximize and close buttons on the upper right, possibly scroll bars on the side and bottom.

     

    In a windows forms application, you put things in this window like buttons, textboxes, etc and write code to make them do things.

     

    Saturday, April 21, 2007 6:41 PM
  • Hi, 2006

    Generally, we put the application entry-- main method into the program.cs and put the logical code in Form1.cs while UI designing code in Form1.Designer.cs. In this way the application is tidy and easy to maintain and reuse.

    Imagine that all the code mixed up in a single file, it would be messy.

    In C# 2.0, partial class is added in. Then we can put single class definition into two or more files to seperate the logic and designer code. It quite make sense, right? In 1.1 or ealier one class must be define in one file, so it looks more messy.

    Of course, you can change the file name to whatever you want but with suffix .cs even not equal to the class name, it's ok. (sometimes we can define multiple classes in single file)

    If you have further questions, please feel free to let me know.

    Thanks

    Monday, April 23, 2007 5:24 AM