Windows Forms applications must declare the [STAThread] attribute; otherwise, the controls might not work correctly. This tells to use single-threaded apartment model instead of multi-threaded. With the SetHighDpiMode method, we ensure that our application looks good on any display resolution. The EnableVisualStyles method enables visual styles.
The application will use the built-in Windows theming to style controls instead of the classic Windows look and feel.
The Run method starts the application. It begins running a standard application message loop on the current thread, and makes the specified form visible. A tooltip is a small rectangular pop-up window that displays a brief description of a control's purpose when the user rests the pointer on the control.
The code example creates a tooltip for two controls: one Button control and the Form control. We place two buttons on the FlowLayoutPanel. It dynamically lays out its contents horizontally or vertically. The default dimension is vertical. We create a new tooltip. A new Button control is created. We set its text with the Text property and size it automatically to fit the text size. Button control represents a Windows button control.
It can be clicked by using the mouse, Enter key, or Spacebar if the button has focus. The example creates a Quit button control; the application terminates when we click on the button. The button has some margin around its borders. We add some space to the left and above the button control. We plug an event handler to the Click event. When we click on the button, the application is closed with the Close method. Since we do not work with the sender object and event arguments, we use discards.
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. A button is used to allow the user to click on a button which would then start the processing of the form. Step 1 The first step is to drag the button control onto the Windows Form from the toolbox as shown below. Step 2 Once the Button has been added, go to the properties window by clicking on the Button control.
Congrats, you now have your first basic Windows Form in place. When working with windows form, you can add events to controls. An event is something that happens when an action is performed. Probably the most common action is the clicking of a button on a form. In C Windows Forms, you can add code which can be used to perform certain actions when a button is pressed on the form.
The below example will showcase an event for the Listbox control. So whenever an item is selected in the listbox control, a message box should pop up which shows the item selected.
Step 1 Double click on the Listbox in the form designer. By doing this, Visual Studio will automatically open up the code file for the form. And it will automatically add an event method to the code. This event method will be triggered, whenever any item in the listbox is selected. With Windows Forms, you can create powerful Windows-based applications.
The following topics describe in-depth how to harness the power of Windows Forms to display data, handle user input, and deploy your applications easily and with enhanced security. Creating a New Windows Form Contains links to topics that describe basic concepts for creating Windows Forms applications.
Changing the Appearance of Windows Forms Contains links to topics that show how to change the appearance of Windows Forms applications. Windows Forms Controls Contains links to topics that describe and show how to use Windows Forms controls and components. User Input in Windows Forms Contains links to topics that describe and show how to handle input from the user in Windows Forms applications.
Dialog Boxes in Windows Forms Contains links to topics that describe the different dialog boxes for use in Windows Forms.
0コメント