2

In his book Erik Brown writes the following code and compiles it from the command-line:

csc MyForm.cs

[assembly: System.Reflection.AssemblyVersion("1.1")]
namespace MyNamespace
{
    public class MyForm : System.Windows.Forms.Form
    {
        public MyForm()
        {
            this.Text = "Hello Form";
        }

        public static void Main()
        {
            System.Windows.Forms.Application.Run(new MyForm());
        }
    }
}

I want to add another form and call it from the first.

Do I need a project file? An assembly file? I don't understand the build process. Can you explain the very basics to me: how do I tell the compiler to build a two-forms application?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Asaf
  • 3,067
  • 11
  • 35
  • 54
  • Not that obvious what that book is trying to teach you. But this code is just criminally wrong. Use a Visual Studio project template to get it right. And get another book. – Hans Passant Feb 04 '17 at 10:23
  • @hans: please dont judge the book that is 99% focus on visual studio and give one example... that answers my unique needs. – Asaf Feb 04 '17 at 11:10
  • Hmm, I'm entitled to judge. A book that teaches very bad programming habits is not a good book. That your UI looks like it came from the previous century is pretty easy to see. But what you cannot see are the very nasty undiagnosable failure you'll get when you keep going. Nobody here will be able to help you either, they won't know that the book told you to do it wrong. Don't do it. – Hans Passant Feb 04 '17 at 11:37

2 Answers2

2

First form (form1.cs):

public class MyForm : System.Windows.Forms.Form
{
    public MyForm()
    {
        this.Text = "Hello Form";
        this.Click += Form_Click;
    }

    public static void Main()
    {
        System.Windows.Forms.Application.Run(new MyForm());
    }

    private void Form_Click(object sender, System.EventArgs e)
    {
        MyForm2 form2 = new MyForm2();
        form2.ShowDialog();
    }
}

Second form (form2.cs):

 public class MyForm2 : System.Windows.Forms.Form
 {
     public MyForm2()
     {
         this.Text = "Hello Form 2";
     }
 }

Now from the command line, locate to the location where you saved these .cs files and then run:

csc form1.cs form2.cs

It will create an EXE file. Run it and click in the form to open form2.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sumit Parakh
  • 1,098
  • 9
  • 19
  • thanks. cant believe its that simple.. I'll check it up soon. So if the csc "knows" how to merge all the files.. what are the assemble and project file, good for? (just curious... you already answered my question).. thanks – Asaf Feb 04 '17 at 11:02
  • Glad it helped. Please upvote it. :) Project file are basically automatically created if you are using visual studio. Project file is not a required component to write and compile c# programs.. – Sumit Parakh Feb 04 '17 at 11:08
2

You need to use Visual Studio (VS) command prompt and mention the C# code file names (*.cs) of all the Windows forms in your project. You must include each form's code-behind file as well as designer code-behind file. Otherise, the compilation will not succeed.

A sample project structure is as below:

WindowsFormsApplication1.csproj
--Program.cs
--Form1.cs
----Form1.Designer.cs
--Form2.cs
----Form2.Designer.cs

The compilation command for above project will look like:

csc /target:winexe Program.cs Form1.cs Form1.Designer.cs Form2.cs Form2.Designer.cs

Note: It is compulsory to include Program.cs file during compilation, else the compiler fails to obtain the project's entry point(the Main method). The /target switch helps you to launch the output EXE file as a GUI based Windows Forms application.

There is a shorter version of the above command which uses wildcard to include all the files names in one go:

csc /target:winexe *.cs

The easiest alternative is to use the msbuild command in place of csc on Visual Studio command prompt. msbuild command can be used to build the project file which contains the reference of all the *.cs files. Here is how the command looks like:

msbuild WindowsFormsApplication1.csproj

This relieves us from mentioning all the C# code file names individually (whether in project root directory or nested sub-direcotries). Build the project file and you are done.

RBT
  • 24,161
  • 21
  • 159
  • 240
  • this can be done without vs with windows command prompt and this was the point. Thanks for giving more info about *.cs. ms build seems like more structured solution but I only managed to build a thin template to keep me going... I never found a stupid proof manual to show me the advanced options: exaples, pictures ;)... anyway thanks... – Asaf Aug 21 '17 at 06:19
  • ok. great. I also wanted to highlight `/target` switch which helps you launch the output exe as a windows forms application. It you don't mention this switch then it will get launched as console application first which in turns lunches the windows application. – RBT Aug 21 '17 at 07:54