banner



How To Create A Windows Application In Visual Studio 2012

So far we have seen how to work with C# to create console based applications. Only in a real-life scenario team normally use Visual Studio and C# to create either Windows Forms or Web-based applications.

A windows form application is an awarding, which is designed to run on a computer. It will not run on web browser because so it becomes a web application.

This Tutorial will focus on how nosotros tin can create Windows-based applications. Nosotros will likewise learn some basics on how to piece of work with the various elements of C# Windows awarding.

In this Windows tutorial, yous will learn-

  • Windows Forms Basics
  • Hello Earth in Windows Forms
  • Adding Controls to a class
  • Event Handling for Controls
  • Tree and PictureBox Control

Windows Forms Basics

A Windows forms application is one that runs on the desktop calculator. A Windows forms awarding volition unremarkably have a drove of controls such as labels, textboxes, list boxes, etc.

Below is an example of a simple Windows grade application C#. It shows a simple Login screen, which is accessible by the user. The user will enter the required credentials and so will click the Login push to go on.

C# Windows Forms Application

Then an case of the controls available in the above application

  1. This is a collection of characterization controls which are usually used to describe adjacent controls. Then in our case, we have 2 textboxes, and the labels are used to tell the user that 1 textbox is for inbound the user proper noun and the other for the password.
  2. The 2 textboxes are used to hold the username and password which will be entered by the user.
  3. Finally, we have the button control. The button control will normally have some code attached to perform a certain prepare of actions. So for example in the above case, we could have the button perform an action of validating the user name and password which is entered by the user.

C# Hello World

At present let's look at an case of how we can implement a unproblematic 'hello world' application in Visual Studio. For this, we would demand to implement the below-mentioned steps

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

C# Windows Forms Application

Pace 2) The adjacent step is to choose the project type as a Windows Forms application. Here we as well demand to mention the name and location of our projection.

C# Windows Forms Application

  1. In the project dialog box, we tin can see various options for creating different types of projects in Visual Studio. Click the Windows option on the left-hand side.
  2. When we click the Windows options in the previous pace, we will be able to run across an selection for Windows Forms Awarding. Click this option.
  3. We will give a name for the application. In our case, it is DemoApplication. We volition also provide a location to shop our application.
  4. Finally, nosotros click the 'OK' push to let Visual Studio create our project.

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

Output:-

C# Windows Forms Application

Yous volition encounter a Grade Designer displayed in Visual Studio. It'south in this Form Designer that y'all will offset building your Windows Forms application.

C# Windows Forms Application

In the Solution Explorer, you will as well exist able to see the DemoApplication Solution. This solution will contain the below 2 project files

  1. A Form awarding called Forms1.cs. This file will incorporate all of the code for the Windows Form application.
  2. The Primary programme chosen Program.cs is default code file which is created when a new application is created in Visual Studio. This code will contain the startup lawmaking for the awarding as a whole.

On the left-hand side of Visual Studio, you will besides encounter a ToolBox. The toolbox contains all the controls which tin 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.

C# Windows Forms Application

Pace three) In this step, we volition now add a characterization to the Course which will display "Howdy World." From the toolbox, you will need to choose the Label command and but drag it onto the Grade.

C# Windows Forms Application

In one case y'all drag the label to the form, y'all can run across the label embedded on the form as shown beneath.

C# Windows Forms Application

Stride 4) The side by side step is to go to the backdrop of the control and Change the text to 'How-do-you-do World'.

To become to the properties of a control, you need to right-click the command and choose the Backdrop bill of fare option

C# Windows Forms Application

  • The backdrop console also shows up in Visual Studio. So for the label command, in the properties control, become to the Text section and enter "Hello World".
  • Each Control has a set of properties which describe the control.

C# Windows Forms Application

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

Output:-

C# Windows Forms Application

In the output, you can meet that the Windows Form is displayed. You can besides encounter 'Hello Globe' is displayed on the course.

Calculation Controls to a form

We had already seen how to add a control to a form when nosotros added the label control in the earlier department to brandish "Hello World."

Let'south wait at the other controls available for Windows forms and see some of their common properties.

In our Windows form application in C# examples, we volition create one grade which will accept the post-obit functionality.

  1. The power for the user to enter name and accost.
  2. An option to choose the metropolis in which the user resides in
  3. The power for the user to enter an option for the gender.
  4. An selection to choose a course which the user wants to learn. There will brand 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.

Group Box

A group box is used for logical grouping controls into a section. Let'southward accept an instance if you lot had a drove of controls for entering details such as name and address of a person. Ideally, these are details of a person, and then you would want to take these details in a separate section on the Form. For this purpose, you tin can have a group box. Let'southward see how we can implement this with an example shown below

Step 1) The kickoff pace is to drag the Groupbox control onto the Windows Course from the toolbox as shown below

C# Windows Forms Application

Step ii) Once the groupbox has been added, become to the properties window by clicking on the groupbox command. In the properties window, go to the Text belongings and modify it to "User Details".

C# Windows Forms Application

Once you lot make the higher up changes, you will see the following output

Output:-

C# Windows Forms Application

In the output, you lot tin can clearly see that the Groupbox was added to the form. Yous can also see that the text of the groupbox was inverse to "User Details."

Label Control

Side by side comes the Label Control. The label control is used to display a text or a message to the user on the form. The characterization command is unremarkably used along with other controls. Common examples are wherein a label is added along with the textbox command.

The label indicates to the user on what is expected to fill up up in the textbox. Let's see how we tin can implement this with an example shown beneath. We volition add 2 labels, one which will be chosen 'name' and the other called 'address.' They will be used in conjunction with the textbox controls which volition be added in the afterwards section.

Step 1) The outset step is to drag the label control on to the Windows Course from the toolbox as shown below. Make sure you drag the characterization control 2 times so that you can have ane for the 'proper name' and the other for the 'accost'.

C# Windows Forms Application

Step two) Once the label has been added, go to the properties window past clicking on the label control. In the properties window, become to the Text property of each label command.

C# Windows Forms Application

Once yous make the higher up changes, you lot volition see the following output

Output:-

C# Windows Forms Application

You can run into the label controls added to the form.

Textbox

A textbox is used for allowing a user to enter some text on the Windows application in C#. Let'south see how we tin implement this with an example shown below. We will add 2 textboxes to the form, one for the Proper noun and the other for the address to be entered for the user

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

C# Windows Forms Application

Step 2) Once the text boxes have been added, get to the properties window past clicking on the textbox control. In the properties window, become to the Name belongings and add a meaningful proper name to each textbox. For case, name the textbox for the user every bit txtName and that for the address as txtAddress. A naming convention and standard should be fabricated for controls because it becomes easier to add actress functionality to these controls, which we will see later on.

C# Windows Forms Application

Once you brand the above changes, y'all will see the following output

Output:-

C# Windows Forms Application

In the output, you can clearly run into that the Textboxes was added to the class.

Listing box

A Listbox is used to showcase a list of items on the Windows form. Permit's run into how we tin can implement this with an example shown below. We will add a list box to the form to store some city locations.

Stride ane) The starting time stride is to drag the list box control onto the Windows Class from the toolbox every bit shown below

C# Windows Forms Application

Stride two) Once the list box has been added, go to the properties window by clicking on the list box control.

C# Windows Forms Application

  1. First, modify the property of the Listbox box control, in our case, we accept changed this to lstCity
  2. Click on the Items belongings. This will allow you to add together different items which tin show upwardly in the list box. In our case, we have selected items "drove".
  3. In the Cord Collection Editor, which pops up, enter the city names. In our case, we have entered "Mumbai", "Bangalore" and "Hyderabad".
  4. Finally, click on the 'OK' push button.

Once you brand the above changes, you will see the following output

Output:-

C# Windows Forms Application

In the output, y'all can run into that the Listbox was added to the form. Yous can besides see that the list box has been populated with the city values.

RadioButton

A Radiobutton is used to showcase a listing of items out of which the user can choose 1. Let's run into how we can implement this with an case shown below. We will add together a radio button for a male person/female option.

Step ane) The get-go step is to elevate the 'radiobutton' command onto the Windows Form from the toolbox as shown below.

C# Windows Forms Application

Pace ii) Once the Radiobutton has been added, become to the properties window by clicking on the Radiobutton control.

C# Windows Forms Application

  1. Outset, you lot need to modify the text property of both Radio controls. Become the properties windows and alter the text to a male person of one radiobutton and the text of the other to female.
  2. Similarly, change the name property of both Radio controls. Go the properties windows and change the name to 'rdMale' of i radiobutton and to 'rdfemale' for the other ane.

1 you make the above changes, you volition encounter the post-obit output

Output:-

C# Windows Forms Application

Y'all will see the Radio buttons added to the Windows course.

Checkbox

A checkbox is used to provide a listing of options in which the user tin can cull multiple choices. Permit's see how we can implement this with an instance shown below. We will add 2 checkboxes to our Windows forms. These checkboxes will provide an pick to the user on whether they want to learn C# or ASP.Net.

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

C# Windows Forms Application

Stride 2) One time the checkbox has been added, go to the properties window past clicking on the Checkbox control.

C# Windows Forms Application

In the backdrop window,

  1. First, you demand to change the text property of both checkbox controls. Go the properties windows and change the text to C# and ASP.Internet.
  2. Similarly, change the name holding of both Radio controls. Go the properties windows and change the name to chkC of ane checkbox and to chkASP for the other 1.

Once y'all make the in a higher place changes, yous will see the post-obit output

Output:-

C# Windows Forms Application

Button

A push is used to allow the user to click on a push which would then start the processing of the form. Let'south run across how we can implement this with an example shown below. We will add a simple push button chosen 'Submit' which volition be used to submit all the information on the form.

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

C# Windows Forms Application

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

C# Windows Forms Application

  1. Starting time, you need to change the text belongings of the button control. Go the properties windows and modify the text to 'submit'.
  2. Similarly, change the proper name property of the control. Get the properties windows and change the name to 'btnSubmit'.

Once you make the in a higher place changes, yous will run across the following output

Output:-

C# Windows Forms Application

Congrats, you now take your starting time basic Windows Form in place. Let's now go to the side by side topic to see how we can do Event treatment for Controls.

C# Upshot Handling for Controls

When working with windows grade, y'all can add together events to controls. An event is something that happens when an action is performed. Probably the most common activity is the clicking of a button on a grade. In C# Windows Forms, you tin can add lawmaking which can be used to perform certain actions when a button is pressed on the form.

Normally when a button is pressed on a class, it means that some processing should take place.

Let's accept a expect at one of the consequence and how it can be handled before we go to the push button event scenario.

The below example volition showcase an result for the Listbox control. So whenever an item is selected in the listbox control, a message box should pop upwardly which shows the particular selected. Let's perform the post-obit steps to achieve this.

Pace ane) Double click on the Listbox in the form designer. By doing this, Visual Studio will automatically open upwards the code file for the grade. And information technology volition automatically add an outcome method to the code. This upshot method will exist triggered, whenever any item in the listbox is selected.

C# Windows Forms Application

In a higher place is the snippet of code which is automatically added past Visual Studio, when yous double-click the List box control on the course. Now permit's add the below section of code to this snippet of lawmaking, to add the required functionality to the listbox event.

C# Windows Forms Application

  1. This is the outcome handler method which is automatically created by Visual Studio when you double-click the List box control. You don't demand to worry almost the complication of the method name or the parameters passed to the method.
  2. Here we are getting the SelectedItem through the lstCity.SelectedItem property. Think that lstCity is the name of our Listbox control. We then employ the GetItemText method to get the actual value of the selected item. Nosotros and so assign this value to the text variable.
  3. Finally, we use the MessageBox method to display the text variable value to the user.

I you make the above changes, and run the program in Visual Studio y'all volition see the following output

Output:-

C# Windows Forms Application

From the output, you can see that when any particular from the list box is selected, a message box will pops upward. This volition show the selected item from the listbox.

Now allow'south look at the final control which is the push click Method. Once again this follows the same philosophy. Just double click the button in the Forms Designer and it will automatically add together the method for the push button outcome handler. Then you lot just need to add the below code.

C# Windows Forms Application

  1. This is the effect handler method which is automatically created by Visual Studio when y'all double click the push control. You lot don't need to worry on the complexity of the method name or the parameters passed to the method.
  2. Here we are getting values entered in the name and accost textbox. The values tin can exist taken from the text property of the textbox. We and so assign the values to two variables, name, and accost appropriately.
  3. Finally, we apply the MessageBox method to brandish the proper name and accost values to the user.

One y'all make the in a higher place changes, and run the plan in Visual Studio you will come across the post-obit output

Output:-

C# Windows Forms Application

  1. Starting time, enter a value in the proper noun and address field.
  2. And then click on the Submit push

Once y'all click the Submit button, a message box will popular, and it volition correctly prove you what y'all entered in the user details department.

Tree and PictureBox Control

At that place are two farther controls we tin can look at, one is the 'Tree Control' and the other is the 'Paradigm control'. Allow's look at examples of how we tin can implement these controls

Tree Command

– The tree control is used to list down items in a tree like way. Probably the all-time example is when we see the Windows Explorer itself. The binder structure in Windows Explorer is like a tree-similar structure.

Let's see how we tin can implement this with an case shown below.

Stride i) The kickoff footstep is to elevate the Tree control onto the Windows Form from the toolbox as shown below

C# Windows Forms Application

Step 2) The next step is to start adding nodes to the tree collection so that it tin can come up up in the tree appropriately. First, let's follow the beneath sub-steps to add a root node to the tree drove.

C# Windows Forms Application

  1. Become to the properties toolbox for the tree view control. Click on the Node's holding. This will bring up the TreeNode Editor
  2. In the TreeNode Editor click on the Add together Root button to add a root node to the tree collection.
  3. Next, change the text of the Root node and provide the text as Root and click 'OK' button. This will add Root node.

Pace 3) The adjacent step is to start adding the child nodes to the tree collection. Allow'southward follow the below sub-steps to add together child root node to the tree collection.

C# Windows Forms Application

  1. First, click on the Add child button. This will allow you to add kid nodes to the Tree collection.
  2. For each kid node, change the text property. Keep on repeating the previous step and this stride and add together 2 boosted nodes. In the end, y'all will have three nodes as shown in a higher place, with the text equally Label, Button, and Checkbox respectively.
  3. Click on the OK button

Once you take fabricated the to a higher place changes, you lot will see the following output.

Output:-

C# Windows Forms Application

You will be able to run into the Tree view added to the form. When yous run the Windows course application, y'all tin can expand the root node and come across the child nodes in the list.

PictureBox Command

This command is used to add images to the Winforms C#. Let'southward see how we can implement this with an example shown below.

Pace 1) The get-go step is to drag the PictureBox command onto the C# Windows Grade from the toolbox every bit shown below

C# Windows Forms Application

Step 2) The next step is to really attach an paradigm to the picture box control. This can exist done by following the beneath steps.

C# Windows Forms Application

  1. First, click on the Image property for the PictureBox control. A new window will pops out.
  2. In this window, click on the Import button. This will be used to attach an prototype to the picturebox control.
  3. A dialog box will popular upwardly in which you will be able to choose the epitome to adhere the picturebox
  4. Click on the OK button

One you make the above changes, you volition run into the post-obit output

Output:-

C# Windows Forms Application

From the output, you can see that an epitome is displayed on the class.

Summary

  • A Windows form in C# application is ane that runs on the desktop of a figurer. Visual Studio Course along with C# tin be used to create a Windows Forms awarding.
  • Controls can be added to the Windows forms C# via the Toolbox in Visual Studio. Controls such as labels, checkboxes, radio buttons, etc. can exist added to the form via the toolbox.
  • One tin besides use advanced controls like the tree view command and the PictureBox control.
  • Event handlers are used to respond to events generated from controls. The near mutual i is the i added for the button clicked effect.

Source: https://www.guru99.com/c-sharp-windows-forms-application.html

Posted by: bowdenheman1981.blogspot.com

0 Response to "How To Create A Windows Application In Visual Studio 2012"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel