Most of the People think that we can’t write C# Windows Forms Applications without using Visual Studio. But this is not true. A C# Application is nothing but a collection of .cs files which we can create just by using using Notepad or any other text editor. In one of my previous article, I discussed about How To Create Hello World! Program In C# Using Notepad. Now in his article, I will show you how can you create a Windows Form Application in C# using nothing but just a Notepad.
- First of all we have to take reference to the Namespaces whose Classes we will be using in this Application. For introduction purpose, I will just create a simple program which will open Windows Form when we run the Program which will be having a specified title. So for these things we just need two namespaces in our Program.
- Now for our Application, we need a Namespace which will be holding two Classes, one is the class which will contain Main function where we call the other class function to show us a Windows Form. So other class will be having code for our Windows Form. You can give any name you want to this Namespace.
- Now within this Namespace, create a Class which will be having Main function where we will call the other Class to show us a Windows Form. Here again you can give this class any name you wish, I giving it name Program here, but the Main function should contain the similar structure I have in the following code.
static void Main()
- Here in above code, I creating a new instance of myForm Class inside Application.Run function, which will run the Application through this Class and our Application will end when this Class instance is disposed off. If you are new to C# then you might not understand what I am saying, So or now just consider this statement as the statement which is telling the Compiler to run the Program through the myForm Class.
- Now I have to create the second Class which is myForm class. You can give it any name as well, but you have to replace myForm everywhere I have used this word with the name you give to this class. Now If we just create a Class like we have done previously, then how the Compiler will consider this class a part of Windows Form Application. In simple words, I have to mention that the Class I am creating is a Windows Forms Class, means this class is inheriting code from the Forms Class. If we will not do this, then Compiler will consider this class to be having code for a Console Application and will show us a number of errors. So you must create this class by writing the syntax the same way as I am writing in the following line of code.
public class myForm : Form
- By writing the : Form after class declaration, we are telling the Compiler that this class is not having code for a Console Application, but for a Windows Forms Application.
- Now we have to create a Constructor here for this class. Now Constructor are the function which always have public scope, have same name as the name of Class, can receive parameters, and these functions gets automatically called when we create New Instance of that Class which we have done in the Application.Run(new myForm()); statement.
- In Windows Forms Applications, Constructor mainly call another method or function which creates all the Controls which we need in our Form and initialize its Properties. Now here I don’t want to create any Control in my Form. I just want to set the title for our Windows Form to some desired value. For that just the following lines of code is fine
this.Text = "HowToIdeas";
- So we are done and we have successfully written a Windows Form Program In C# using Notepad only. You have to save this file with any name but must set its extension to .cs. Here is the snapshot of the code that I have written in Notepad.
- Now its time to test this Program i.e to Compile and Run this Program. Now here I am just posing the snapshot of Visual Studio Command Prompt where I am Compiling and Running the Program. If you don’t know how to Compile and Run C# Programs using Visual Studio Command Prompt, then you can see my article on How To Compile C# Application Using Visual Studio Command Prompt.
Now you must try to Compile the Program by not writing that : Form text while declaring 2nd Class which we have done in 5th step. And you will be getting a bunch of errors in that case.
Incoming search terms:
- call another form c# different namespace (1)