Menus provide groups of related commands for Windows Form
s apps.
An expanded menu lists menu items and submenus.
A menu that contains a menu item is called that menu item’s parent menu. A menu item that contains a submenu is considered to be the parent of that submenu.
All menus and menu items can have shortcut keys.
Some menu items display checkmarks, indicating that multiple options on the menu can be selected at once.
The MenuStrip
control is used to create menus in a GUI.
Top-level menus and their menu items are represented using type ToolStripMenuItem
.
To create an access shortcut, type an ampersand (&
) before the character to be underlined.
To add other shortcut keys, set the ShortcutKeys
property of the ToolStripMenuItem
.
You can hide shortcut keys by setting property ShowShortcutKeys
to false
. You can modify how shortcut keys are displayed in the menu item by modifying property ShortcutKeyDisplayString
.
A menu item’s Checked
property is used to display a check to the left of the menu item.
MonthCalendar
ControlThe MonthCalendar
control displays a monthly calendar.
The user can select a date from the currently displayed month or navigate to another month.
A MonthCalendar
’s DateChanged
event occurs when a new date is selected.
DateTimePicker
ControlThe DateTimePicker
control can be used to retrieve date and/or time information from the user.
Property Format
of class DateTimePicker
specifies the user’s selection options.
The DateTimePicker
’s ValueChanged
event is raised when the selected value is changed.
LinkLabel
ControlThe LinkLabel
control displays links to other resources, such as files or web pages.
A LinkLabel
appears as underlined text (colored blue by default). When the mouse moves over the link, the pointer changes to a hand; this is similar to a hyperlink in a web page.
The link can change color to indicate whether the link is new, previously visited or active.
When clicked, the LinkLabel
generates a LinkClicked
event.
ListBox
ControlThe ListBox
control allows the user to view and select items in a list.
ListBox
property SelectionMode
determines the number of items that can be selected.
The SelectedIndexChanged
event of class ListBox
occurs when the user selects a new item.
Property Items
returns all the list items as a collection.
Property SelectedItem
returns the currently selected item.
Use method Add
to add an item to the ListBox
’s Items
collection.
You can add items to ListBox
es and CheckedListBox
es visually by using the Items
property in the Properties window.
CheckedListBox
ControlThe CheckedListBox
control extends a ListBox
by including a checkbox next to each item.
Items can be added via methods Add
and AddRange
or through the String Collection Editor.
CheckedListBox
es imply that multiple items can be checked.
CheckedListBox
event ItemCheck
occurs when a user checks or unchecks a CheckedListBox
item.
ComboBox
ControlThe ComboBox
control combines TextBox
features with a drop-down list.
Property MaxDropDownItems
specifies the maximum number of items that can display at one time.
You can add objects to collection Items
programmatically, using methods Add
and AddRange
, or visually, with the String Collection Editor.
Property DropDownStyle
determines the type of ComboBox
and is represented as a value of the ComboBoxStyle
enumeration, which contains values Simple
, DropDown
and DropDownList
.
There can be at most one selected item in a ComboBox
(if none, then SelectedIndex
is -1
).
When the selected item changes in a ComboBox
, a SelectedIndexChanged
event occurs.
TreeView
ControlThe TreeView
control displays nodes hierarchically in a tree.
Traditionally, nodes are objects that contain values and can refer to other nodes.
A parent node contains child nodes, and the child nodes can be parents to other nodes.
Two child nodes that have the same parent node are considered sibling nodes.
A tree is a collection of nodes, usually organized in a hierarchical manner. The first parent node of a tree is a root node—there can be multiple root nodes.
TreeView
controls are useful for displaying hierarchical information.
In a TreeView
, a parent node can be expanded or collapsed by clicking the plus box or minus box to its left. Nodes without children do not have these boxes.
The nodes displayed in a TreeView
are instances of class TreeNode
.
Each TreeNode
has a Nodes
collection (type TreeNodeCollection
), containing a list of TreeNodes
.
To add nodes to a TreeView
visually, click the ellipsis next to property Nodes
in the Properties window. This opens the TreeNode Editor, which displays an empty tree representing the TreeView
.
To add nodes programmatically, you must create a root TreeNode
object and pass it a string
to display. Then call method Add
to add this new TreeNode
to the TreeView
’s Nodes
collection.
ListView
ControlThe ListView
control is similar to a ListBox
in that both display lists from which the user can select one or more items. ListView
is more flexible and can display items in different formats.
Property MultiSelect
(a bool
) determines whether multiple items can be selected.
To display images, an ImageList
component is required.
Property SmallImageList
of class ListView
sets the ImageList
for the small icons.
Property LargeImageList
of class ListView
sets the ImageList
for large icons.
The items in a ListView
are each of type ListViewItem
.
TabControl
ControlThe TabControl
control creates tabbed windows.
TabControl
s contain TabPage
objects. Only one TabPage
is displayed at a time.
You can add TabControl
s visually by dragging and dropping them on a Form
in Design mode.
To add TabPage
s in Design mode, open the TabControl
’s smart tasks menu and click Add Tab, or click the TabPages
property in the Properties window, and add tabs in the dialog that appears.
Each TabPage
raises a Click
event when its tab is clicked.
The app window of a multiple document interface (MDI) program is called the parent window, and each window inside the app is referred to as a child window.
Child windows cannot be parents themselves and cannot be moved outside their parent.
To create an MDI Form
, create a new Form
and set its IsMdiContainer
property to true
.
To add a child Form
to the parent, create a new child Form
object, set its MdiParent
property to the parent Form
and call the child Form
’s Show
method.
Property MdiWindowListItem
of class MenuStrip
specifies which menu, if any, displays a list of open child windows.
MDI containers allow you to organize the placement of child windows. The child windows in an MDI app can be arranged by calling method LayoutMdi
of the parent Form
.
Visual inheritance allows you to create a new Form
by inheriting from an existing Form
. The derived Form
class contains the functionality of its base class.
Visual inheritance also can be applied with other controls as well.
Visual inheritance enables you to achieve visual consistency across apps by reusing code.
A reusable class is typically placed in a class library.
When you compile a class library, the compiler will create a .dll
file, known as a dynamically linked library—a way to package classes that you can reference from other apps.
The .NET Framework allows you to create custom controls.
Custom controls can appear in the user’s Toolbox and can be added to Form
s, Panel
s or Group-Box
es in the same way that Button
s, Label
s and other predefined controls are added.
The simplest way to create a custom control is to derive a class from an existing control, such as a Label
. This is useful if you want to add functionality to an existing control, rather than replacing it with one that provides the desired functionality.
To create a new control composed of existing controls, use class UserControl
.
Controls added to a custom control are called constituent controls.
A programmer can create a brand-new control by inheriting from class Control
. This class does not define any specific behavior; that task is left to you.
Timer
s are non-visual components that generate Tick
events at a set interval. This interval is set by the Timer
’s Interval
property, which defines the number of milliseconds (thousandths of a second) between events. Timers are disabled by default.