Lect Note On Chapter 3 - Part II - Event-Driven Component
Lect Note On Chapter 3 - Part II - Event-Driven Component
Menus
LinkLabels
ListBoxes and CheckedListBoxes
ComboBoxes
TreeViews
ListViews
Tab Control
Multiple-Document-Interface (MDI) Windows
Common dialog
Menus
• Menus are used to provide groups of related commands for Windows
applications.
• An expanded menu lists various commands (called menu items), plus
submenus (menus within a menu).
• top-level menus appear in the left portion of the figure, whereas any
submenus or menu items are displayed to the right.
• The menu that contains a menu item is called parent menu.
• A menu item that contains a submenu is considered to be the parent of that
submenu.
• All menu items can have Alt key shortcuts (also called access shortcuts or hot
keys),
– accessed by pressing Alt and the underlined letter (for example, Alt + F retrieves the
File menu).
• Menus that are not top-level menus can have shortcut keys as well
(combinations of Ctrl, Shift, Alt, F1, F2, letter keys etc.).
• Some menu items display checkmarks, usually indicating that multiple
options on the menu can be selected at once.
• To create a menu, open the Toolbox, and drag a MainMenu
control onto the form.
• This creates a menu bar on the top of the form and places a
MainMenu icon underneath it.
• To select the MainMenu, click the icon. This setup is known
as the Visual Studio .NET Menu Designer, which allows the
user to create and edit menus.
• Menu have properties, which can be accessed through the
Properties window or the Menu Designer, and events, which
can be accessed through the Class Name and Method Name
drop-down menus.
• To add entries to the menu – click Type Here textbox and type the text that should appear in
the menu.
• Each entry in the menu is of type MenuItem from the System.Windows.Forms namespace.
The menu itself is of type MainMenu.
• Pressing the Enter key, add the menu item.
• Then, more Type Here textboxes appear, allowing us to add items underneath or to the side
of the original menu item.
• To create an access shortcut - type an ampersand (&) in front of the character to be
underlined.
– E.g. to create the File menu item, type &File.
• The actual ampersand character is displayed by typing &&.
• To add other shortcut keys (such as Ctrl + F9), set the Shortcut property of the MenuItem.
• Programmers can remove a menu item by selecting it with the mouse and pressing the Delete
key.
• Separator bars are inserted by right-clicking the menu and selecting Insert Separator or by
typing “-” as the menu text.
• Menu items generate a Click event when selected.
• To create an empty event handler, enter code-view mode, double click on the MenuItem in
design view.
• Menus can also display the names of open windows in multiple-document-interface (MDI)
forms (see Section 13.9). Menu properties and events are summarized in Fig. 13.3.
LinkLabels
• Class LinkLabel is derived from class Label and
therefore inherits all of class Label’s
functionality.
• The LinkLabel control displays links to other
objects, such as files or Web pages.
• A LinkLabel appears as underlined text (colored
blue by default).
• similar to the behavior of a hyperlink in a Web
page.
• When clicked, the LinkLabel generates a
LinkClicked event.
• The event handlers for the LinkLabel instances call static method Start of
class Process (namespace System.Diagnostics).
– This method allows us to execute other programs from our application.
– Method Start can take as arguments either the file to open (a String) or the
name of the application to run and its command-line arguments (two Strings).
– Method Start’s arguments can be in the same form as if they were provided
for input to the Run command in Windows.
– To open a file that has a file type that Windows recognizes, simply insert the
file’s full path name.
– The Windows operating system should be able to use the application
associated with the given file’s extension to open the file.
• Demo – the following application contains three
linklabels to link to the:
– C:\drive, the yahoo mail page (www.mail.yahoo.com) and
the Notepad application, respectively.
– The Text properties of the LinkLabels:
driveLinkLabel, yahooLinkLabel and
notepadLinkLabel are set to describe each link’s
purpose
// browse C:\ drive
private void driveLinkLabel_LinkClicked( object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs
e)
{
driveLinkLabel.LinkVisited = true;
System.Diagnostics.Process.Start( "C:\\" );
}
– Unless SelectedIndex is -1 , the handler removes the item of the selected index.
private void removeButton_Click( object sender, System.EventArgs e ) // remove item if one selected
{
// remove only if item selected
if ( displayListBox.SelectedIndex != -1 )
displayListBox.Items.RemoveAt(displayListBox.SelectedIndex );
}
{
displayListBox.Items.Clear();
}
• Finally, event handler exitButton_Click terminates the application, using method Application.Exit.
private void exitButton_Click( object sender, System.EventArgs e ) //exit application
{
Application.Exit();
}
CheckedListBoxes
• CheckedListBox control derives from class ListBox and includes a
checkbox.
• As in ListBoxes, items can be added via methods Add and AddRange or
through the String Collection Editor.
• CheckedListBoxes imply that multiple items can be selected,
– possible values for the SelectionMode property are SelectionMode.None and
SelectionMode.One.
• SelectionMode.One allows multiple selection,
• Thus, the only choice is whether to give the user multiple selection or no
selection at all.
• This keeps the CheckedListBox’s behavior consistent with that of
CheckBoxes.
• The programmer is unable to set the last two SelectionMode values,
MultiSimple and MultiExtended, because the only logical selection
modes are handled by None and One.
• Common properties and events of CheckedListBoxes appear in Fig.
13.12.
• Event ItemCheck is generated whenever a user checks or
unchecks a CheckedListBox item.
• Event argument properties CurrentValue and NewValue
return CheckState values for the current and the new state
of the item, respectively.
• A comparison of these values allows us to determine
whether the CheckedListBox item was checked or
unchecked.
• The CheckedListBox control retains the SelectedItems and
SelectedIndices properties (inherits from class ListBox).
• However, it also includes properties CheckedItems and
CheckedIndices, which return information about the
checked items and indices.
• The following example uses a CheckedListBox and a
ListBox to display a user’s selection of books.
• The CheckedListBox named inputCheckedListBox
allows the user to select multiple titles.
• Using String Collection Editor add the following items:
– C++, Java, VB, Internet & WWW, Perl, Python, Wireless
Internet and Advanced Java.
• The ListBox, named displayListBox – displays the
user’s selection.
• In the screen shots accompanying this example, the
CheckedListBox appears to the left, the ListBox to the
right.
• When the user checks or unchecks an item in
CheckedListBox inputCheckedListBox, the system generates
an ItemCheck event.
• Event handler inputCheckedListBox_ItemCheck handles
the event.
• An if/else control structure determines whether the user
checked or unchecked an item in the CheckedListBox.
• The program uses the NewValue property to test for
whether the item is being checked (CheckState.Checked).
• If the user checks an item, adds the checked entry to the
ListBox displayListBox.
• If the user unchecks an item, line 41 removes the
corresponding item from displayListBox.
// item about to change, add or remove from displayListBox
private void inputCheckedListBox_ItemCheck( object sender,
System.Windows.Forms.ItemCheckEventArgs e )
{
// obtain reference of selected item
string item = inputCheckedListBox.SelectedItem.ToString();
// if item checked add to listbox otherwise remove from listbox
if ( e.NewValue == CheckState.Checked )
displayListBox.Items.Add( item );
else
displayListBox.Items.Remove( item );
}
ComboBoxes
• ComboBox control - combines TextBox features with a
drop-down list.
• A drop-down list is a GUI component that contains a list
from which values can be chosen.
• It usually appears as a text box with a down arrow to its
right.
• By default, the user can enter text into the text box or
click the down arrow to display a list of predefined items.
• If a user chooses an element from this list, that element is
displayed in the text box.
• The maximum number of items that a drop-down list can
display at one time is set by property
MaxDropDownItems.
sample ComboBox in three different states.
• You can add objects to collection Items
programmatically, using methods Add and
AddRange, or visually, with the String
Collection Editor.
• Class ComboBoxTest (Fig. 13.16) allows users
to select a shape to draw—an empty or filled
circle, ellipse, square or pie—by using a
ComboBox.
• The combo box in this example is uneditable,
so the user cannot input a custom item.
• Add items Circle, Square, Ellipse, Pie, Filled
Circle, Filled Square, Filled Ellipse and Filled
Pie to the Items collection.
• Whenever the user selects an item from imageComboBox, the
system generates a SelectedIndexChanged event.
• Event handler imageComboBox_SelectedIndexChanged handles
these events.
• create a Graphics object, a Pen and a SolidBrush, with which the
program draws on the form.
• The Graphics object allows a pen or brush to draw on a
component, using one of several Graphics methods.
• The Pen object is used by methods drawEllipse, drawRectangle
and drawPie to draw the outlines of their corresponding shapes.
• The SolidBrush object is used by methods fillEllipse, fillRectangle
and fillPie to draw their corresponding solid shapes.
• Colors the entire form White, using Graphics method Clear.
// create graphics object, pen and brush
Graphics myGraphics = base.CreateGraphics();