How To Ideas | How To Articles | How To Tutorials


3

How To Create A Simple Windows Forms Application In C# Using Notepad


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.

Instructions:

  1. 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.

    using System;
    using System.Windows.Forms;

  2. 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.

    namespace HowToIdeas
    {

    }

  3. 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.

    class Program
    {
        static void Main()
        {
            Application.Run(new myForm());
        }
    }

  4. 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.
  5. 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
    {
    }

  6. 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.
  7. 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.
  8. 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

    public myForm()
    {
        this.Text = "HowToIdeas";
    }

  9. 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.
    Windows Form Application In C# Using Notepad
  10. 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.
    Windows Form Application In C# Using Notepad

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)
Filed in: C#, Notepad Tags: , , , , , , , , , , ,

3 Responses to "How To Create A Simple Windows Forms Application In C# Using Notepad"

  1. shilpa says:

    thanks…

  2. Yogesh Chauhan says:

    This is good

  3. Meena says:

    nice… article… this basic is required for GUI beginners… or else they will be depended on IDE… without knowing what is happening in background…

Leave a Reply

Submit Comment



© 4094 How To Ideas. All rights reserved.
Proudly designed by Theme Junkie.