Title: Additional Windows Controls
1Chapter 13
- Additional Windows Controls
2Objectives
- Describe the purpose of container controls and
the relationships between them - Create a menu system for a form
- Create a toolbar on a form
- Display informational messages using the
StatusStrip control - Restrict character input using the MaskedTextBox
control - Use the RichTextBox control to work with
formatted text
3Working with Additional Windows Controls
- The MenuStrip control is used to create a menu
system - The ContextMenuStrip control is used to create
pop-up (context) menus - The ToolStrip control is used to create a toolbar
- The StatusStrip control is used to create a
status bar - The MaskedTextBox supports editing capabilities
beyond those supported by the TextBox control - The RichTextBox control is used to edit Rich Text
Format files and plain text files
4Understanding the Purpose of Container Controls
- As their name implies container controls contain
(group) other control instances - The TabControl is a container control
- The TabPage control is a container control
- The MenuStrip, ToolStrip, and StatusStrip
controls are also container controls
5Figure 13-1 Hierarchical relationship between
container control instances
6Creating Menus
- Menus provide an alternative to buttons on a form
- A menu improves the user interface when too many
buttons may clutter a user interface - A menu systems contains
- Drop-down menus
- Menu items
7Figure 13-2 Anatomy of a menu
8Classes that make up a Menu System
- The MenuStrip class provides the container for
the menu - The ToolStripMenuItem class is used to create
items on a menu - The ToolStripComboBox class creates a drop-down
combo box on a menu - The ToolStripTextBox class creates a text box on
a menu - The ToolStripSeperator class creates a visual
separator bar
9Introduction to the MenuStrip Class
- The MenuStrip class is responsible for the layout
of menu items - The MenuStrip class supports the following
members - The Items property is a collection containing
references to the items appearing on the menu - The ShowItemToolTips property controls whether
ToolTips appear - The TextDirection property allows a menu to be
rotated - The Visible property defines whether the menu and
menu items are visible
10Introduction to the ToolStripMenuItem Class
- The ToolStripMenuItem class is the primary class
used to create menu items - The Checked property defines a checked menu item
- The Enabled property enables or disables a menu
item - The Image property contains the menu items image
- The ShowShortcutKeys and ShortcutKeys properties
define the shortcut keys for a menu item - The Text property defines the text appearing on a
menu item - The Visible property defines whether the menu
item is visible
11Creating a Menu System
- Create an instance of the MenuStrip control on a
form - The MenuStrip control appears on the Menus
Toolbars tab of the Toolbox - Using the Windows Forms Designer, edit the menu
system in place - Edit properties with the Properties window or
Items Collection Editor
12Figure 13-3Menu visual designer
13Figure 13-4 Menu visual designer with menu items
created
14Associating a Menu System with a Form
- A form may have several menu systems
- Associate a menu system with a form by setting
the forms MainMenuStrip property to a MenuStrip
instance - Example
- Me.MainMenuStrip msDemo
15Modifying a Menu System
- Right-click a menu item or menu and select items
from the context menu - Click Delete to delete a menu item
- Click Insert to insert a menu or menu item
- Click Insert, Separator to insert a separator bar
- Click View Code to view the Code Editor for a
menu item's Click event handler - Click Properties to activate the Properties
window for the menu item - The Set Image button assigns an image to a menu
item
16The Items Collection Editor
- The Items Collection Editor provides an
alternative to editing menu items in-place - The Items Collection Editor can only edit part of
a menu system at once - Click Add to add a menu item
- Reorder menu items, as necessary
- Use the Properties section to edit a menu item's
properties - These same properties can be edited using the
Properties window
17Figure 13-5Items Collection Editor
18Shortcut Keys
- Create shortcut keys for the more frequently used
menu items - Shortcut keys can reduce the number of keyboard
to mouse transitions thereby improving the user
interface - The first character of a menu system is typically
used as shortcut key
19Figure 13-6 Creating a shortcut key for a menu
item
20Adding Images to Menu Items
- Each menu item can display an image
- Images are commonly used by other control
instances too - Use the Select Resource dialog box to add images
- Images can be stored in two ways
- In the projects resource file
- In the resource file for the form
21Figure 13-7 Select Resource dialog box
22Creating Event Handlers for Menus
- Event handlers for menus work like event handlers
for buttons - Both support a Click event
- Double-click a menu item to create the Click
event handler and activate the Code Editor
23Creating Context Menus
- Use the ContextMenuStrip control to create a
context menu - Create menu items in-place as usual
- Create Click event handlers for menu items
- Associate a context menu with a control instance
by setting the ContextMenuStrip property of the
control instance
24Figure 13-8Creating a context menu
25Creating a Toolbar (Introduction)
- The ToolStrip control provides the container for
the buttons that appear on the toolbar - Controls that appear on a toolbar
- ToolStripButton
- ToolStripSeparator
- ToolStripDropDownButton
- TolStripCombobox
- ToolStripTextBox
26Figure 13-10Toolbar with sample buttons
27Creating Buttons on a Toolbar
- Use the Items Collection Editor to edit the
buttons on a toolbar - Its the same Items Collection Editor used with
other control instances - Set properties for each button, as necessary
28Figure 13-11Items Collection Editor
29The ToolStripButton Class
- The ToolStripButton is similar to an ordinary
Button - Properties
- The Checked property is used to create a checked
button - The CheckOnClick property, when True, causes the
button to become checked when clicked - The DisplayStyle property cause the button to
display text, an image, or both - The Image property contains the image appearing
on the button - The Text property contains the text appearing on
the button - Handle the Click event
30The ToolStripSeparator Class
- Use to create a vertical space between two
buttons - Properties
- The ForeColor property defines the color of the
separator bar - The Visible property defines whether the
separator bar is visible or hidden
31The ToolStripProgressBar Class
- The ToolStripProgressBar displays a progress bar
used to indicate the relative progress of a
long-running operation - The Minimum and Maximum properties define the
range of values - The Value property contains the current value
- The Increment method accepts one argument the
value to increment the progress bar
32Other ToolStrip Controls
- The ToolStripComboBox displays a combo box
- The ToolStripTextBox displays an editable text
box - The ToolStripLabel displays a label
33Introduction to the StatusStrip Control
- The StatusStrip control is used to create a
status bar typically appearing along the bottom
of a form - Its a container for other control instances
- ToolStripStatusLabel
- ToolStripProgressBar
- ToolStripDropDownButton
34Figure 13-12 StatusStrip with sample control
instances
35The Items Collection Editor
- Again, the Items Collection Editor is used to
create the items on a status bar - The interface is the same as other Collection
Editors
36Items Collection Editor for the StatusStrip
37The ToolStripLabel Class
- The ToolStripLabel class provides the primary
means to display items on a status bar - Properties
- The BorderSides property defines how the border
appears - The BorderStyle property defines how the
three-dimensional border appears - The Padding property controls the white space
between the text and the edge of the control
instance - The Spring and AutoSize properties control
automatic resizing
38Displaying the Status of Keyboard Keys
- The My.Computer.Keyboard class has properties to
get the status of keyboard keys - NumLock
- CapsLock
- ScrollLock
39Displaying the Status of Keyboard Keys (Example)
- Display the status of the Num Lock key
- If My.Computer.Keyboard.NumLock True Then
- tsslNumLock.Text "Num enabled"
- Else
- tsslNumLock.Text "Num disabled"
- End If
40Advanced Text Processing Controls (Introduction)
- Two controls extend the capabilities of the
TextBox control - The MaskedTextBox control supplies specialized
editing capabilities - The RichTextBox control is used to edit a file
format called Rich Text Format (RTF)
41The MaskedTextBox Control
- The Mask property is used to restrict valid input
characters and the position of those characters - Its similar to the Format argument of the
ToString method - Special characters called placeholder characters
define the input mask
42Placeholder and Literal Characters (1)
43Placeholder and Literal Characters (2)
44Creating an Input Mask
- Set the Mask property by hand or with code
- Use the Input Mask dialog box
- Visual Studio supplies several common input masks
45Figure 13-15Input Mask Dialog Box
46Additional Properties of the MaskedTextBox Control
- The PromptChar property stores the character that
will be displayed as a prompt - The BeepOnError property controls whether an
audible beep is sounded if an invalid character
is entered - The MaskInputRejected event fires when an invalid
character is entered
47Introduction to the RichTextBox Control
- Use the RichTextBox control to edit files having
a standardized format of RTF - RTF files contain formatting directives
48Figure 13-18Rich text file
49Figure 13-18 Formatted text in a RichTextBox
control instance
50Configuring the Appearance of the RichTextBox
Control
- The AcceptsTab defines what happens when the user
presses the Tab key - If True, a Tab character is inserted
- If False, pressing the Tab key causes the next
control instance to get focus - The DetectURLs property controls whether URLs are
recognized - The RightMargin property sets the right margin
for text - The ScrollBars property controls whether
horizontal or vertical scroll bars appear
51Reading and Writing Files with the RichTextBox
Control
- The LoadFile and SaveFile methods read and write
text files and rich text files - There is no need to read files one character or
one line at a time
52LoadFile and SaveFile (Syntax)
- The path argument contains the filename
- The LoadFile and SaveFile methods read and write
the file, respectively - The enumerations PlainText and RichText control
how the file is processed
53The LoadFile and SaveFile Methods (Example)
- Load and save an RTF file
- rtfText.LoadFile("C\Demo.rtf", _
RichTextBoxStreamType.RichText) - rtfText.SaveFile("C\Demo.rtf", _
- RichTextBoxStreamType.RichText)
54Managing Text in a RichTextBox Control
- Two concepts are important related to selecting
text - When no text is selected, the cursor position is
called the insertion point - When the end user selects text, that text is
called the selection
55Properties Related to the Insertion Point or
Selection
- The SelectedRTF property contains the selected
text including formatting directives - The SelectedText property contains the selected
text without the formatting directives - The SelectionColor property contains the
foreground color of the selected text - The SelectionFont property contains the font of
the selected text