Step 1) The first step involves the creation of a new project in Visual Studio. After launching Visual Studio, you need to choose the menu option New->Project.

a1

Step 2) The next step is to choose the project type as a Windows Forms application. Here we also need to mention the name and location of our project.

a2

  • In the project dialog box, we can see various options for creating different types of projects in Visual Studio. Click the Windows option on the left-hand side.
  • When we click the Windows options in the previous step, we will be able to see an option for Windows Forms Application. Click this option.
  • We then give a name for the application which in our case is DemoApplication. We also need to provide a location to store our application.
  • Finally, we click the ‘OK’ button to let Visual Studio to create our project.

If the above steps are followed, you will get the below output in Visual Studio.

Output:-

a3

You will actually see a Form Designer displayed in Visual Studio. It’s on this Form Designer that you will start building your Windows Forms application.

a4

In the Solution explorer, you will also be able to see the DemoApplication Solution. This solution will contain the below 2 project files

  • A Form application called Forms1.cs. This file will contain all of the code for the Windows Form application.
  • The Main program called Program.cs is default code file which is created when a new application is created in Visual Studio. This code will contain the startup code for the application as a whole.

On the right-hand side of Visual Studio, you will also see a ToolBox. The toolbox contains all the controls which can be added to a Windows Forms. Controls like a text box or a label are just some of the controls which can be added to a Windows Forms.

Below is a screenshot of how the Toolbox looks like.

a5

Step 3) In this step, we will now add a label to the Form which will display “Customer.” From the toolbox , you will need to choose the Label control and simply drag it onto the Form.

a6

Once you drag the label to the form, you can actually see the label embedded on the form as shown below.

a7

Step 4) The next step is to actually go to the properties of the control and Change the text to ‘Hello World’.

To go to the properties of control, you need to right-click the control and choose the Properties menu option

a8

  • The properties panel also shows up in Visual Studio. So for the label control, in the properties control, go to the Text section and enter “Customer”.
  • Each Control has a set of properties which describe the control.

a9

If you follow all of the above steps and run your program in Visual Studio, you will get the following output

Output:-

a10

In the output, you can clearly see that the Windows Form is displayed. You can also see ‘Customer’ is displayed on the form.AND

Adding Controls to a form

We had already seen how to add a control to a form when we added the label control in the earlier section to display “Customer.”

The ability for the user to enter name and address.

  • An option to choose the city in which the user resides in
  • The ability for the user to enter an option for the gender.
  • An option to choose a course which the user wants to learn. There will choices for both C# and ASP.Net

So let’s look at each control in detail and add them to build the form with the above-mentioned functionality.

Step 1) The first step is to drag the ListBox control onto the Windows Form from the toolbox as shown below

a11

Step 2) Once the ListBox has been added, go to the properties window by clicking on the ListBox control. In the properties window, go to the Text property and change it to “User Details”.

a12

One you make the above changes, you will see the following output

Output:-

a13

Label Control – Next comes the Label Control. The label control is used to display a text or a message to the user on the form. The label control is normally used along with other controls

The label gives an indication to the user on what is expected to fill up in the textbox. Let’s see how we can implement this with an example shown below. We will add 4 labels,   They will be used in conjunction with the textbox controls which will be added in the later section.

Step 1) The first step is to drag the label control on to the Windows Form from the toolbox as shown below. Make sure you drag the label control 4 times

a14

Step 2) Once the label has been added, go to the properties window by clicking on the label control. In the properties window, go to the Text property of each label control.

a15

One you make the above changes, you will see the following output

Output:-

a16

You can actually see the label controls added to the form.

  • Textbox– A textbox is used for allowing a user to enter some text on the forms application. Let’s see how we can implement this with an example shown below. We will add 2 textboxes to the form , one for the Name and the other for the address to be entered for the user

Step 1) The first step is to drag the textbox control onto the Windows Form from the toolbox as shown below

a17

Step 2) Once the text boxes have been added, go to the properties window by clicking on the textbox control.

a18

One you make the above changes, you will see the following output

Output:-

a19

In the output, you can clearly see that the Textboxes was added to the form.

Button – A button is used to allow the user to click on a button which would then start the processing of the form. Let’s see how we can implement this with an example shown below. We will add a simple button called ‘Submit’ which will be used to submit all the information on the form.

Step 1) The first step is to drag the button control onto the Windows Form from the toolbox as shown below

a20

Step 2) Once the Button has been added, go to the properties window by clicking on the Button control.

a21

  • First, you need to change the text property of the button control. Go the properties windows and change the text to ‘submit’.
  • Similarly, change the name property of the control. Go the properties windows and change the name to ‘btnSubmit’.

One you make the above changes, you will see the following output

Output:-

a22

on button click

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplicationDemo
{
    public partial class CustomerUI : Form
    {
        public CustomerUI()
        {
           InitializeComponent();
        }
     private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtCustomerName.Text.Length == 0)
            {
                MessageBox.Show("Name is required");
                txtCustomerName.Focus();
                return;
            }
            if (txtMobileName.Text.Length == 0)
            {
                MessageBox.Show("Mobile Number is required");
                txtCustomerName.Focus();
                return;
            }
            if (txtProductName.Text.Length == 0)
            {
                MessageBox.Show("Product Name is required");
               txtCustomerName.Focus();
                return;
            }
            if (txtProductAmount.Text.Length == 0)

            {
                MessageBox.Show("Product Amount is required");
               txtCustomerName.Focus();
                return;
            }
        }
}

Business layer logic

After that create the new project  class library to rename ” business layer logic”

Go and Add one more project will be Class libeary and name is BisunessLayerLogic. this is not related to UI.

a23

using System;
using System.Collections.Generic;
First think id we need to go and import the namespace "using BisunessLayerLogic".
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusineesLogis
{
    public class Customer
    {
        public string CustomerName { get; set; }
        public string PhoneNumber { get; set; }
        public string ProductName { get; set; }
        public decimal ProductAmount { get; set; }
        public void validate()
        {
            if (CustomerName.Length == 0)
            {
                throw new Exception("customer Name is required");
            }
            if (PhoneNumber.Length == 0)
            {
                throw new Exception("Phone Number is required");
            }
            if (ProductName.Length == 0)
            {
                throw new Exception("Product Name is required");
            }
            if (ProductAmount == 0)
            {
               throw new Exception("Product Amount is required");
            }
        }
    }
}

a24

First think id we need to go and import the namespace “using BisunessLayerLogic”.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BusinessLogis;
namespace WindowsFormsApplicationDemo
{
    public partial class CustomerUI : Form
    {
       public CustomerUI()
        {
            InitializeComponent();
        }
       private void btnSave_Click(object sender, EventArgs e)
      {
      try
           {
                Customer c_obj = new Customer();
                c_obj.CustomerName = txtCustomerName.Text;
                c_obj.MobileNumber = txtMobileNumber.Text;
                c_obj.ProductName = txtProductName.Text;
                c_obj.ProductAmount = Convert.ToDecimal(txtProdecctAmount.Text);
                c_obj.validate();
           }
           catch (Exception ex)
            {
               MessageBox.Show(ex.Message);
            }
        }
    }
}

Output

a25

If I want to us my reference in console Application then create a new Project.

And use as a reference in console apllication.

a26

Create Project

a27

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogis;
namespace ConsoleAppBusinessLayer
{
    class Program
    {
        static void Main(string[] args)
        {
          try
            {
                Customer obj = new Customer();

                obj.CustomerName = Console.WriteLine();

                obj.PhoneNumber = Console.WriteLine();

                obj.ProductName =Console.WriteLine();

                obj.ProductAmount = Console.WriteLine();

                obj.validate();

            }

            catch( Exception exc)

            {

               Console.WriteLine (exc message);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogis
{
    public class Customer
    {
        public string CustomerName { get; set; }
        public string PhoneNumber { get; set; }
        public string ProductName { get; set; }
        public decimal ProductAmount { get; set; }
        public Customer()
        {
            CustomerName = "";
            MobileName ="";
            ProductName ="";
            ProductAmount = 0;
       }
        public void validate()
        {
            if (CustomerName.Length == 0)
            {
               throw new Exception("customer Name is required");
            }
            if (PhoneNumber.Length == 0)
            {
                throw new Exception("Phone Number is required");
            }
            if (ProductName.Length == 0)
            {
               throw new Exception("Product Name is required");
            }
            if (ProductAmount == 0)
            {
                throw new Exception("Product Amount is required");
            }
        }
    }
}

a28

 

 

Advertisements