How To Make Windows Form Application In C#
So far we have seen how to work with C# to create console based applications. But in a real-life scenario squad unremarkably utilise Visual Studio and C# to create either Windows Forms or Web-based applications.
A windows class application is an application, which is designed to run on a computer. Information technology will not run on web browser considering then it becomes a web application.
This Tutorial will focus on how we can create Windows-based applications. We volition also acquire some basics on how to work with the various elements of C# Windows application.
In this Windows tutorial, you will learn-
- Windows Forms Basics
- Hullo Globe in Windows Forms
- Adding Controls to a form
- Result Treatment for Controls
- Tree and PictureBox Command
Windows Forms Basics
A Windows forms awarding is one that runs on the desktop reckoner. A Windows forms application will ordinarily have a collection of controls such as labels, textboxes, list boxes, etc.
Below is an example of a simple Windows course application C#. It shows a simple Login screen, which is accessible by the user. The user volition enter the required credentials and and so will click the Login button to proceed.
So an example of the controls bachelor in the above awarding
- This is a collection of characterization controls which are normally used to describe next controls. So in our case, nosotros take 2 textboxes, and the labels are used to tell the user that one textbox is for entering the user proper name and the other for the countersign.
- The 2 textboxes are used to concord the username and password which will exist entered by the user.
- Finally, we have the button control. The button control volition unremarkably accept some code attached to perform a certain set of actions. So for example in the above case, we could take the button perform an activity of validating the user name and countersign which is entered by the user.
C# Hello World
Now let's look at an example of how we tin can implement a simple 'howdy world' application in Visual Studio. For this, we would need to implement the beneath-mentioned steps
Footstep ane) 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->Projection.
Pace two) The next footstep is to choose the project type as a Windows Forms application. Here we also need to mention the name and location of our projection.
- In the project dialog box, nosotros can come across 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 stride, we volition be able to meet an selection for Windows Forms Awarding. Click this option.
- We volition give a name for the application. In our instance, information technology is DemoApplication. Nosotros will likewise provide a location to store our application.
- Finally, we click the 'OK' button to let Visual Studio create our project.
If the higher up steps are followed, you will get the below output in Visual Studio.
Output:-
Y'all will see a Form Designer displayed in Visual Studio. It's in this Form Designer that you will get-go edifice your Windows Forms application.
In the Solution Explorer, you will also be able to see the DemoApplication Solution. This solution will contain the below 2 project files
- A Course awarding chosen Forms1.cs. This file will incorporate all of the lawmaking for the Windows Form application.
- The Main programme called Plan.cs is default code file which is created when a new application is created in Visual Studio. This lawmaking will contain the startup code for the application equally a whole.
On the left-paw side of Visual Studio, you will likewise run across 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.
Beneath is a screenshot of how the Toolbox looks similar.
Step 3) In this step, we will now add together a characterization to the Class which will display "Hello World." From the toolbox, you lot volition need to choose the Label control and just drag it onto the Form.
In one case y'all drag the characterization to the class, you tin can see the characterization embedded on the form as shown below.
Pace iv) The side by side step is to become to the properties of the control and Change the text to 'Hello World'.
To go to the properties of a control, you need to right-click the control and choose the Properties bill of fare option
- The backdrop panel likewise shows upwardly in Visual Studio. Then for the label control, in the properties command, go to the Text section and enter "Hullo Earth".
- Each Control has a prepare of backdrop which describe the control.
If yous follow all of the above steps and run your program in Visual Studio, you will go the following output
Output:-
In the output, you lot can see that the Windows Form is displayed. You tin also see 'Hello World' is displayed on the class.
Calculation Controls to a class
We had already seen how to add a command to a form when we added the label control in the earlier section to display "Hello Globe."
Let's await at the other controls available for Windows forms and encounter some of their common backdrop.
In our Windows form application in C# examples, nosotros will create one form which volition have the following functionality.
- The power 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 pick for the gender.
- An option to choose a class which the user wants to learn. In that location will make choices for both C# and ASP.Net
And so let's look at each control in item and add them to build the form with the above-mentioned functionality.
Group Box
A group box is used for logical group controls into a section. Let's take an case if yous had a collection of controls for inbound details such every bit name and address of a person. Ideally, these are details of a person, so you would want to have these details in a divide department on the Form. For this purpose, yous can have a grouping box. Let's come across how nosotros can implement this with an example shown below
Footstep 1) The beginning step is to drag the Groupbox command onto the Windows Course from the toolbox every bit shown below
Step 2) In one case the groupbox has been added, go to the backdrop window by clicking on the groupbox control. In the backdrop window, go to the Text property and change it to "User Details".
Once you lot brand the in a higher place changes, yous will see the post-obit output
Output:-
In the output, you tin clearly encounter that the Groupbox was added to the form. You tin too run into that the text of the groupbox was changed to "User Details."
Label Command
Next comes the Characterization Control. The label control is used to display a text or a message to the user on the course. The label command is normally used along with other controls. Common examples are wherein a label is added along with the textbox control.
The characterization indicates to the user on what is expected to make full upwardly in the textbox. Let's see how we can implement this with an example shown beneath. We volition add together 2 labels, one which will be called 'name' and the other called 'accost.' They will be used in conjunction with the textbox controls which volition be added in the later section.
Step 1) The offset step is to drag the label command on to the Windows Form from the toolbox as shown beneath. Brand certain you elevate the label command 2 times so that you tin have 1 for the 'name' and the other for the 'address'.
Step 2) Once the label has been added, get to the properties window by clicking on the label control. In the properties window, go to the Text property of each characterization control.
Once you lot make the to a higher place changes, y'all will see the post-obit output
Output:-
You tin can encounter the characterization controls added to the course.
Textbox
A textbox is used for allowing a user to enter some text on the Windows application in C#. Let's come across how we can implement this with an case shown below. We will add 2 textboxes to the course, one for the Name and the other for the address to be entered for the user
Step 1) The beginning footstep is to drag the textbox control onto the Windows Form from the toolbox as shown beneath
Step 2) Once the text boxes accept been added, go to the properties window by clicking on the textbox control. In the properties window, get to the Name property and add a meaningful name to each textbox. For example, proper name the textbox for the user as txtName and that for the address as txtAddress. A naming convention and standard should be made for controls considering it becomes easier to add extra functionality to these controls, which nosotros will see later on.
Once you make the above changes, y'all will see the following output
Output:-
In the output, y'all can conspicuously see that the Textboxes was added to the class.
List box
A Listbox is used to showcase a list of items on the Windows form. Let's see how we tin implement this with an case shown below. We volition add a list box to the form to store some city locations.
Pace 1) The commencement step is to drag the list box control onto the Windows Form from the toolbox every bit shown below
Step ii) In one case the list box has been added, go to the properties window by clicking on the list box command.
- Showtime, change the holding of the Listbox box control, in our case, we have inverse this to lstCity
- Click on the Items property. This will allow you to add different items which tin can show upwards in the list box. In our case, we take selected items "collection".
- In the Cord Collection Editor, which pops up, enter the urban center names. In our case, we have entered "Mumbai", "Bangalore" and "Hyderabad".
- Finally, click on the 'OK' push.
Once y'all make the above changes, you will see the post-obit output
Output:-
In the output, you can see that the Listbox was added to the class. Yous can also 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 one. Let'due south encounter how nosotros tin can implement this with an example shown below. We will add together a radio button for a male/female selection.
Step one) The first stride is to drag the 'radiobutton' control onto the Windows Form from the toolbox equally shown below.
Step ii) Once the Radiobutton has been added, go to the properties window by clicking on the Radiobutton control.
- First, you need to change the text property of both Radio controls. Become the backdrop windows and change the text to a male of one radiobutton and the text of the other to female.
- Similarly, alter the name property of both Radio controls. Go the properties windows and change the proper noun to 'rdMale' of one radiobutton and to 'rdfemale' for the other ane.
I you brand the in a higher place changes, you will come across the following output
Output:-
You volition see the Radio buttons added to the Windows class.
Checkbox
A checkbox is used to provide a list of options in which the user can choose multiple choices. Permit's see how we tin implement this with an example shown below. We volition add together two checkboxes to our Windows forms. These checkboxes volition provide an pick to the user on whether they want to acquire C# or ASP.Net.
Pace 1) The first step is to drag the checkbox control onto the Windows Course from the toolbox as shown below
Step 2) Once the checkbox has been added, go to the backdrop window past clicking on the Checkbox control.
In the backdrop window,
- First, you need to change the text property of both checkbox controls. Go the backdrop windows and change the text to C# and ASP.Cyberspace.
- Similarly, modify the proper noun property of both Radio controls. Go the properties windows and change the proper name to chkC of ane checkbox and to chkASP for the other one.
One time yous make the above changes, yous volition see the following output
Output:-
Button
A button is used to let the user to click on a button which would and so start the processing of the grade. Permit'due south run into how we can implement this with an example shown below. We will add a simple button called 'Submit' which will exist used to submit all the information on the course.
Step 1) The first footstep is to drag the button command onto the Windows Form from the toolbox as shown below
Footstep ii) Once the Push button has been added, become to the backdrop window by clicking on the Push control.
- Starting time, y'all need to change the text property of the button command. Get the properties windows and alter the text to 'submit'.
- Similarly, change the name holding of the control. Go the properties windows and change the proper noun to 'btnSubmit'.
Once you make the above changes, yous will see the post-obit output
Output:-
Congrats, you lot now have your beginning bones Windows Form in place. Let's now go to the next topic to see how nosotros tin can do Consequence handling for Controls.
C# Event Handling for Controls
When working with windows class, you can add events to controls. An event is something that happens when an action is performed. Probably the almost mutual activeness is the clicking of a button on a form. In C# Windows Forms, you lot can add together code which can be used to perform certain deportment when a button is pressed on the class.
Ordinarily when a push is pressed on a grade, it ways that some processing should have place.
Let's take a look at 1 of the event and how it can be handled earlier we go to the button event scenario.
The below example will showcase an effect for the Listbox control. And so whenever an particular is selected in the listbox control, a message box should pop upwardly which shows the item selected. Let's perform the following steps to achieve this.
Pace 1) Double click on the Listbox in the form designer. By doing this, Visual Studio will automatically open up upward the code file for the form. And information technology will automatically add an event method to the code. This event method volition be triggered, whenever any item in the listbox is selected.
Above is the snippet of code which is automatically added past Visual Studio, when you double-click the List box control on the course. Now let's add the below department of code to this snippet of lawmaking, to add together the required functionality to the listbox event.
- This is the event handler method which is automatically created by Visual Studio when you double-click the Listing box control. You don't demand to worry about the complication of the method name or the parameters passed to the method.
- Hither we are getting the SelectedItem through the lstCity.SelectedItem holding. Remember that lstCity is the proper noun of our Listbox control. Nosotros and then use the GetItemText method to get the actual value of the selected particular. We so assign this value to the text variable.
- Finally, we employ the MessageBox method to brandish the text variable value to the user.
1 you make the above changes, and run the program in Visual Studio y'all volition run across the following output
Output:-
From the output, you can see that when any detail from the listing box is selected, a message box will pops up. This will bear witness the selected item from the listbox.
At present permit's await at the final control which is the push button click Method. Over again this follows the same philosophy. Simply double click the button in the Forms Designer and it will automatically add the method for the button consequence handler. And then you lot simply need to add together the below code.
- This is the consequence handler method which is automatically created by Visual Studio when you double click the push control. You don't need to worry on the complication of the method name or the parameters passed to the method.
- Here nosotros are getting values entered in the name and address textbox. The values can be taken from the text property of the textbox. We and then assign the values to 2 variables, name, and address accordingly.
- Finally, we utilise the MessageBox method to display the name and accost values to the user.
One you brand the higher up changes, and run the plan in Visual Studio you will meet the following output
Output:-
- First, enter a value in the name and address field.
- Then click on the Submit button
Once you click the Submit push button, a bulletin box will popular, and information technology will correctly testify you what you entered in the user details section.
Tree and PictureBox Command
There are 2 farther controls nosotros can look at, one is the 'Tree Control' and the other is the 'Image control'. Let's expect at examples of how nosotros tin implement these controls
Tree Control
– The tree command is used to list downwardly items in a tree like fashion. Probably the best example is when we see the Windows Explorer itself. The folder structure in Windows Explorer is like a tree-like structure.
Permit's see how we can implement this with an example shown beneath.
Step 1) The offset step is to drag the Tree control onto the Windows Form from the toolbox as shown below
Step 2) The next step is to start adding nodes to the tree drove so that it can come up in the tree accordingly. First, let'due south follow the below sub-steps to add together a root node to the tree collection.
- Go to the properties toolbox for the tree view command. Click on the Node'south belongings. This will bring upwards the TreeNode Editor
- In the TreeNode Editor click on the Add Root button to add a root node to the tree collection.
- Next, modify the text of the Root node and provide the text as Root and click 'OK' push button. This will add together Root node.
Footstep 3) The next step is to start adding the child nodes to the tree collection. Let's follow the below sub-steps to add child root node to the tree collection.
- First, click on the Add child push button. This will allow yous to add kid nodes to the Tree drove.
- For each child node, change the text property. Keep on repeating the previous step and this stride and add two additional nodes. In the stop, you lot volition have iii nodes as shown above, with the text every bit Label, Push, and Checkbox respectively.
- Click on the OK push
Once you take made the in a higher place changes, you will run into the following output.
Output:-
You will be able to see the Tree view added to the class. When y'all run the Windows form application, you lot can expand the root node and see the kid nodes in the listing.
PictureBox Command
This control is used to add images to the Winforms C#. Let's see how nosotros tin can implement this with an instance shown below.
Footstep i) The first pace is to drag the PictureBox control onto the C# Windows Class from the toolbox equally shown below
Footstep 2) The next step is to actually adhere an image to the moving-picture show box command. This tin can be done by following the below steps.
- Offset, click on the Image belongings for the PictureBox control. A new window volition pops out.
- In this window, click on the Import button. This volition exist used to adhere an image to the picturebox control.
- A dialog box will pop up in which you will be able to choose the image to attach the picturebox
- Click on the OK button
One yous brand the above changes, you volition run into the post-obit output
Output:-
From the output, you can see that an image is displayed on the form.
Summary
- A Windows form in C# application is one that runs on the desktop of a computer. Visual Studio Form forth with C# can 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. tin can be added to the form via the toolbox.
- One tin can as well apply advanced controls like the tree view command and the PictureBox control.
- Event handlers are used to respond to events generated from controls. The most common one is the ane added for the push clicked event.
Source: https://www.guru99.com/c-sharp-windows-forms-application.html
Posted by: williamsonmese1948.blogspot.com
0 Response to "How To Make Windows Form Application In C#"
Post a Comment