Creating windows form application c#


















In our Windows form application in C examples, we will create one form which will have the following functionality. A group box is used for logical grouping controls into a section. Ideally, these are details of a person, so you would want to have these details in a separate section on the Form.

For this purpose, you can have a group box. Step 1 The first step is to drag the Groupbox control onto the Windows Form from the toolbox as shown below. Step 2 Once the groupbox has been added, go to the properties window by clicking on the groupbox control. In the output, you can clearly see that the Groupbox was added to the form.

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. Common examples are wherein a label is added along with the textbox control. The label indicates to the user on what is expected to fill up in the textbox. Step 1 The first step is to drag the label control on to the Windows Form from the toolbox as shown below. 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. A textbox is used for allowing a user to enter some text on the Windows application in C. 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. Step 2 Once the text boxes have been added, go to the properties window by clicking on the textbox control.

In the properties window, go to the Name property and add a meaningful name to each textbox. For example, 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 because it becomes easier to add extra functionality to these controls, which we will see later on. A Listbox is used to showcase a list of items on the Windows form. We will add a list box to the form to store some city locations.

Step 1 The first step is to drag the list box control onto the Windows Form from the toolbox as shown below. Step 2 Once the list box has been added, go to the properties window by clicking on the list box control. In the output, you can see that the Listbox was added to the form. You can also see that the list box has been populated with the city values. A Radiobutton is used to showcase a list of items out of which the user can choose one.

Step 2 Once the Radiobutton has been added, go to the properties window by clicking on the Radiobutton control. A checkbox is used to provide a list of options in which the user can choose multiple choices. We will add 2 checkboxes to our Windows forms. These checkboxes will provide an option to the user on whether they want to learn C or ASP. Step 1 The first step is to drag the checkbox control onto the Windows Form from the toolbox as shown below.

Step 2 Once the checkbox has been added, go to the properties window by clicking on the Checkbox control. The Dock property allows you to attach a control to one of the edges of its parent. The term parent applies to Windows Forms forms, because Windows Forms forms contain the controls that you drag and drop on them.

By default, the Dock property of any control is set to None. For example, a control docked to the top edge of a form will always be connected to the top edge of the form, and it will automatically resize in the left and right directions when its parent is resized. The Dock property for a control can be set by using the provided graphical interface in the Properties window, as shown in Figure Setting the Dock property.

When a user resizes a form, the controls maintain a constant distance from the edges of their parent form using the Anchor property. The default value for the Anchor property for any control is set to Top, Left, that means that this control will maintain a constant distance from the top and left edges of the form.

The Anchor property can be set by using the provided graphical interface in the Properties window, as shown in Figure Because of the default setting of the Anchor property to Top, Left, if you try to resize a form by stretching it toward the right side, you will see that its controls are still positioned on the left rather than shifting to the center of the form to adjust to the size of the form after resizing is done. If opposite edges, for example, Left and Right, are both set in the Anchor property, the control will stretch when the form is resized.

However, if neither of the opposite edges is set in the Anchor property, the control will float when the parent is resized. Setting the Anchor property. In this exercise, you will use the existing Windows Forms application named WinApp, that you created previously in the chapter. You will see how to modify this application in such a way that when you resize the form, its controls behave accordingly and keep the application presentable for the user. When you resize the form, it will behave according to the settings of the Dock and Anchor properties.

In the first instance, you set the Dock property of the Label control to Top, that allows this Label control to be affixed to the top border of the form and span the entire width of the form. Setting the Anchor property of the remaining controls to Top, Left, and Right shifts the controls in such a manner that they will maintain a constant distance from the left and right borders of the form.

Adding a New Form to the Project Any real-world or enterprise application will obviously need multiple Windows Forms forms to perform business functionality. By default, every project opens with only one Windows Forms form, but you are free to add more. In this new form, you will add data from two different text boxes to a ListBox and a ComboBox, respectively. Keep your current project open, because you'll need it immediately for the next exercise.

Don't worry, we'll explain how this and the next exercise works afterward. Setting the start-up form in a Visual C project is a little tricky, so I wanted to break it into its own exercise. To set a start-up form, you need to follow these steps:.

In the project you modified in the previous exercise, navigate to Solution Explorer, open the Program. Run new WinApp ;. This code line ensures the WinApp form will be the first form to run all the time because this is the first form called Form1, that was already added and you renamed it when you began this project; in order to set the UserInfo form as the start-up form, you need to modify this statement a little, as follows:.

Run new UserInfo ;. The UserInfo application form will be loaded. Enter a country and state name in the appropriate text boxes and click the Add button; you will see that the names you entered have been added to the ListBox and ComboBox, as shown in Figure The ListBox and ComboBox controls have a collection named Items, and this collection can contain a list of items, that is why you use it here.

Add txtCountry. Text ; Also, once an item is added, for a better user experience, it is advisable to clear the text box so a user can type in a new value with ease. Clear ;. Add txtState. Text ; txtState. Clear ; For the "Remove Country" and "Remove State" buttons, you follow a similar approach as the Items collection, but this time instead of "Add " you call the "Remove " method, and as you know, the prerequisites for an item removal is that an item must be selected first either in the ListBox or the ComboBox.

Hence, the code seeks a SelectedItem to be passed to the Remove method. Remove lstCountry. SelectedItem ; cboState. Remove cboState. SelectedItem ; Now for the "Show Details" button, you have used some conditional logic to produce a different message based on your selection, specifically with the Male and Female radio buttons.

The CheckBox and RadioButton controls offer a property named Checked, that can be either true or false, that is, checked or not checked. You build a condition around these and then show a message box. View All. Vidya Vrat Agarwal Updated date Aug 08, User Interface Design Principles The best mechanism for interacting with any application is often a user interface.

Best Practices for User Interface Design The user interface provides a mechanism for users to interact with your application. Simplicity Simplicity is an important aspect of a user interface. Position of Controls The location of controls on your user interface should reflect their relative importance and frequency of use. Consistency Your user interface should exhibit a consistent design across each form in your application.

Aesthetics Whenever possible, a user interface should be inviting and pleasant. Color Judicious use of color helps make your user interface attractive to the target audience and inviting to use. Fonts Usability should determine the fonts you choose for your application.

Images and Icons Pictures and icons add visual interest to your application, but careful design is essential to their use. Sorting Properties in the Properties Window Each object such as a form control has many properties you may need to set while working with an application. Categorized View The Categorized View organizes properties in the form of sets of properties, and each set has a name to describe that collection of properties; for example, there are categories named Appearance, Behavior, Data, Design, Focus, and so on.

Categorized View of properties Alphabetical View The Alphabetical View organizes properties in ascending order by name from a to z. Alphabetical View of properties Setting the Properties of Solutions, Projects, and Windows Forms forms Before you begin putting controls onto the Windows Forms form, you need to learn how to modify some property values of the solution, project, and form you created earlier shown previously in Figure IDE after setting the properties for your solution, project, and Windows Forms form Working with Controls Now that you have your Windows Forms application in place, you can start working with the controls.

Go to the project named WinApp located under the solution, that you created earlier refer to Figure Ensure that you are in the Design View. Drag a Label control onto the form, and position it at the top middle of the form. Select this label, navigate to the Properties window, and set the following properties: Set the Name property to lblWelcome.

Set the Text property to Welcome. Select the Font property, click the ellipsis button, and specify the size of the Label control as 16 points in the Size drop-down list.

Set the TextAlign property to TopCenter. Drag two more Label controls onto the form, and put them below the "Welcome" text, a little toward the left of the form. Select the first label, navigate to the Properties window, and set the Name property to lblFirstName and the Text property to First Name. Now select the second label, navigate to the Properties window, and set its Name property to lblLastName and its Text property to Last Name. Select textBox1, go to the Properties window, and set its Name property to txtFname.

Select textBox2, and in the Properties window set its Name property to txtLname. Drag a Button control onto the form, and place it below the Label and TextBox controls. Select the Button control, go to the Properties window, change the Name property to btnSubmit, and then set its Text property to Submit.

Now you have your GUI design of the application ready; it should resemble the form shown in Figure Now let's add the following code inside this btnSubmitClick event to do the desired functionality of a dialog, with a message, a caption in the dialog box's title bar, an OK button, a Cancel button, and an information icon displayed: MessageBox. Information ; Now your Code View will show the button's click event code, as shown in Figure To provide the functionality for this control, you add the following code: MessageBox.

Information ; "MessageBox. Dock Property The Dock property allows you to attach a control to one of the edges of its parent. Setting the Dock property Anchor Property When a user resizes a form, the controls maintain a constant distance from the edges of their parent form using the Anchor property.

Go to Solution Explorer and open the WinApp project. Open the WinApp form in the Design View. Select the form by clicking its title bar; you will see handles around the form's border, that allow you to resize the form's height and width.

Place the cursor on the handle of the right border, and when the mouse pointer becomes double-headed, click and stretch the form toward the right side. You will see that the form's width increases, but the controls are still attached to the left corner of the form.

Similarly, grab the handle located on the bottom of the form and try to increase the height of the form. You will notice that the controls are still attached to the top side of the form. Have a look at Figure , that shows a resized height and width form and the position of the controls. The controls appear in the top-left corner because their Dock property values are None and their Anchor property values are Top, Left.

Resized form and position of controls Now you will try to set the Dock and Anchor properties for the controls and then retest the application. Select the Label control named lbl Welcome, and set the Text value to Welcome. Go to the Properties window. Select the AutoSize property, and set its value to False default value is True. Resize the width of the Label control to the width of the form, and adjust the Label control to the top border of the form.

Set this control's TextAlign property to Top, Center. Set the Dock property for the Label control from None to Top, that means you want the label to always be affixed with the top border of the form. Now select all the remaining controls two Labels, two TextBoxes, and one Button either by scrolling over all of them while holding down the left mouse button or by selecting each with a click while pressing down either the Shift or Ctrl key.

Once you have selected all the controls, go to the Properties window. You will see listed all the properties common to the controls you have selected on the form. This will allow you to adjust the controls accordingly as soon as you resize the form. The controls will also grow in size accordingly to adjust to the width of the form, as you can see in Figure The effect of the Anchor property setting Top, Left, Right on a resized form Note: The Anchor property has very interesting behaviors; you can try setting this property in various combinations and see the effects when you resize your form.

Return the form to its previous size so you can see the effects of setting another Anchor property. Select all the controls again as you did in step 8. Set the Anchor property to Top only and try resizing the form now. You will notice that the controls are floating in the middle of the form when you resize it, as you can see in Figure The effect of the Anchor property setting Top on a resized form Save the changes in your project by clicking "File" "Save All".

How It Works When you resize the form, it will behave according to the settings of the Dock and Anchor properties. This will add a new Windows Forms form to your project. See Modify Visual Studio. Expand the SQL Server node.

Create a new Windows Forms App. Name the project WindowsSearchForm. This step creates a data source from a database using the Data Source Configuration wizard:. If a data connection to the Northwind sample database is available in the drop-down list, select it. If your database requires a password, select the option to include sensitive data, and then click Next.

On the Save connection string to the Application Configuration file page, click Next. On the Choose your Database Objects page, expand the Tables node. You can create the data-bound controls by dragging items from the Data Sources window onto your form:.

Make sure the Windows Forms designer has the active focus and the Data Sources window is open and pinned. Drag the Customers node from the Data Sources window to your form. Just below the design surface for your form, select the customersTableAdapter button, and then in the Properties window, choose Add Query The data grid is populated with customers that meet the criteria.

In this example, the data grid only displays customers that have a value of London in their City column. Depending on your application requirements, there are several steps you may want to perform after creating a parameterized form.



0コメント

  • 1000 / 1000