C sharp (C#)programming languages

C# Windows Forms Application: How to create First Window Form Application in C#

C# Windows Forms Application

C# Windows Forms Application:- Today, most applications have graphical user interfaces. These differ from console applications not only through their window-based interface but also through much closer collaboration with the underlying operating system. While console application simply sends their output to the console and input from the keyboard, C# Windows Forms applications present a graphical user interface (Graphical User Interface, GUI) form windows, controls, and plots that the user uses to with the mouse, keyboard, and any other input devices. This tutorial describes how to create a simple c# Windows Forms application.


C# Windows Forms Creating:

Step 1: Create a project

  • In the File menu, select the New / Project command.
  • In the New Project dialog box that appears, select Visual C # / Windows as the project type and then select the Windows Forms application as a template.
  • Enter HelloWorldGUI as the name for the project and choose a location that suits you and clear the Create Solution Directory check box. Visual C # creates a separate subdirectory for the project under the selected storage location, which bears the name of the project. If you also have a project folder created, the project folder is saved as a subdirectory under the specified storage location and the project directory as a subdirectory of the project folder furnished. If you clear the option for the solution directory, the project directory becomes created directly under the selected storage location.
  • Confirm the dialog box with OK.

C# Windows Forms Application

C# Windows Forms Application

The Visual C# development environment now creates the project, including two source code files:

  • cs defines a Form1 class for the main application window.
  • cs contains the Main () method in which the main window is created and used with the application is connected.

The Form1.cs file is automatically loaded for editing, but not in the code editor, but in the form designer.



Step 2: edit the form

To get some routine with the form designer, we will now use the title of the Change window, insert a label control to display a welcome text and a button Offer to exit the application. First, let’s set up the IDE for further work with the designer:

  • Move the mouse over the toolbox bar on the left IDE frame and then press Open Toolbox window, click the pushpin icon to keep the Toolbox window open permanently. (If the toolbox is not displayed in the IDE frame, open it with View / Toolbox.)

C# Windows Forms Application

  • Open the properties window (command View / Properties window).

Next we change the title and dimensions of the window:

  • In the Form Designer, click the window to select it. The properties window is now linked to the Form1 window.
  • Scroll down the Properties window to the Text property and enter in the right Enter the new text HelloWorldGUI  in the input field next to it.
  • Back in the Form Designer, pick up the check box in the lower right corner and move it to the top right until the window is narrower and about twice as wide as it is tall.

In a non-editable text field (label), we output a short greeting text:

  • Use the mouse to drag a label control from the toolbox into the window.
  • In the properties window, change the text of the label to “Hello world!”.
  • In the Properties window, select a larger font for the Label control.
  • Center the Label control with the Format / Center on Form / Horizontal command. You can set the vertical position with the cursor down / up keys, after which you can press Leave a little more space at the bottom for the OK button.



Finally, in addition to the Close button on the title bar, we also offer an OK button to exit the application.

  • Use the mouse to drag a button control from the toolbox into the window.
  • In the properties window, change the text of the label to “OK”.
  • In the Properties window, select a slightly larger font for the button control. Fit Size of the control, if necessary.
  • Move the Button control to the lower right corner.
  • In the Form Designer, double-click the Button control to display its Click event with a To let the treatment method connect. The treatment method is automatically created as a member of the Form1 class and processed loaded into the code editor. To close the application, you can call the Application.Exit () method or – as in the following listing – close the main application form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelloWorldGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

      
    }
}


Step 3: compile

To compile the project, all you need to do is call the corresponding Build command.

  • Compile the project using the Build / Build HelloWorldGUI command.

If the compiler detects that the source text is syntactically incorrect or that there are suspect area contains the error list window with error messages and warnings. Could Project can be created without any problems, the message Creation successful appears in the status bar.

Step 4: run

Once the compilation is successful, you can run and test the application. To do this, you have to Don’t even leave the Visual C# environment, just invoke the Debug / Start command without debugging on. Of course, the application can also be started from the operating system level. The .exe file can be found in the project subdirectory in my case the path is C:\Users\Fawad khan\documents\visual studio 2010\Projects, if You created the project in the release or another configuration You can start it directly from Windows Explorer by double-clicking.



Final Output of C# Windows Forms Application:

C# Windows Forms Application

As you can see in the bottom error list there is no error is found and the form opened finely. When you press the ok button the form will be closed.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button