8.introduction To C# Window Form Application
8.introduction To C# Window Form Application
Here the solution is like a container which contains the projects and
files that may be required by the program.
• After that following window will display which will be divided into three
parts as follows:
1. Editor Window or Main Window: Here, you will work with forms
and code editing. You can notice the layout of form which is now
blank. You will double click the form then it will open the code for
that.
2. Solution Explorer Window: It is used to navigate between all items
in solution. For example, if you will select a file form this window
then particular information will be display in the property window.
3. Properties Window: This window is used to change the different
properties of the selected item in the Solution Explorer. Also, you
can change the properties of components or controls that you will
add to the forms.
Now to add the controls to your WinForms application go
to Toolbox tab present in the extreme left side of Visual Studio. Here,
you can see a list of controls. To access the most commonly used
controls, go to Common Controls present in Toolbox tab.
• Now drag and drop the controls that you needed on created Form. For
example, if you can add Textbox, List Box, Button etc. as shown below.
By clicking on the particular dropped control, you can see and change
its properties present in the right most corner of Visual Studio.
• In the above image, you can see the Textbox is selected and its
properties like TextAlign, MaxLength etc. are opened in right most
corner. You can change its properties’ values as per the application
need. The code of controls will be automatically added in the
background. You can check the Form1.Designer.cs file present in the
Solution Explorer Window.
• To run the program, you can use an F5 key or Play button present in
the toolbar of Visual Studio. To stop the program you can use pause
button present in the Toolbar. You can also run the program by going
to Debug->Start Debugging menu in the menubar.
Introduction to windows form controls
In Windows app development, a control is a UI element that displays
content or enables interaction. You create the UI for your app by using
controls such as buttons, text boxes, and combo boxes to display data and
get user input.
Add a control
• Use a design tool like Blend for the Microsoft Visual Studio Extensible
Application Markup Language (XAML) designer.
• Add the control to the XAML markup in the Visual Studio XAML editor.
• Add the control in code. Controls that you add in code are visible when
the app runs, but are not visible in the Visual Studio XAML designer.
The Visual Studio Toolbox displays many of the controls that you can use in
your app. To add a control to your app, double-click it in the Toolbox. For
example, when you double-click the TextBox control, this XAML is added to
the XAML view.
You can also drag the control from the Toolbox to the XAML designer.
Set the name of a control
To work with a control in code, you set its x:Name attribute and reference it
by name in your code. You can set the name in the Visual Studio Properties
window or in XAML. Here's how to set the name of the currently selected
control by using the Name text box at the top of the Properties window.
To name a control
You use properties to specify the appearance, content, and other attributes
of controls. When you add a control using a design tool, some properties
that control size, position, and content might be set for you by Visual
Studio. You can change some properties, such as Width, Height or Margin,
by selecting and manipulating the control in the Design view.
To reset a property
1. In the Properties panel, click the property marker next to the property
value. The property menu opens.
2. In the property menu, click Reset
Each control has events that enable you to respond to actions from your
user or other changes in your app. For example, a Button control has a Click
event that is raised when a user clicks the Button. You create a method,
called an event handler, to handle the event. You can associate a control's
event with an event handler method in the Properties window, in XAML, or
in code.
To create an event handler, select the control and then click the Events tab
at the top of the Properties window. The Properties window lists all of the
events available for that control. Here are some of the events for a Button.
Don't use a Button control when the action is to navigate to another page;
instead, use a HyperlinkButton control.
Check boxes
A check box is used to select or deselect action items. It can be used for a
single item or for a list of multiple items that a user can choose from. The
control has three selection states: unselected, selected, and indeterminate.
Use the indeterminate state when a collection of sub-choices have both
unselected and selected states.
When the combo box is closed, it either displays the current selection or is
empty if there is no selected item. When the user expands the combo box,
it displays the list of selectable items.
Hyperlinks
Hyperlinks navigate the user to another part of the app, to another app, or
launch a specific uniform resource identifier (URI) using a separate browser
app. There are two ways that you can add a hyperlink to a XAML app:
the Hyperlink text element and HyperlinkButton control. Use a hyperlink
when you need text that responds when pressed and navigates the user to
more information about the text that was pressed.
Radio buttons
Radio buttons, also called option buttons, let users select one option from a
collection of two or more mutually exclusive, but related, options. Radio
buttons are always used in groups, and each option is represented by one
radio button in the group.
Use radio buttons to let users select from two or more mutually exclusive
options.
Sliders
A slider is a control that lets the user select from a range of values by
moving a thumb control along a track.
Use a slider when you want your users to be able to set defined, contiguous
values (such as volume or brightness) or a range of discrete values (such as
screen resolution settings).