Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2002 Prentice Hall. All rights reserved. 1 Chapter 13 – Graphical User Interfaces: Part 2 Outline 13.1Introduction 13.2 Menus 13.3 LinkLabel s 13.4 ListBox.

Similar presentations


Presentation on theme: " 2002 Prentice Hall. All rights reserved. 1 Chapter 13 – Graphical User Interfaces: Part 2 Outline 13.1Introduction 13.2 Menus 13.3 LinkLabel s 13.4 ListBox."— Presentation transcript:

1  2002 Prentice Hall. All rights reserved. 1 Chapter 13 – Graphical User Interfaces: Part 2 Outline 13.1Introduction 13.2 Menus 13.3 LinkLabel s 13.4 ListBox es and CheckedListBox es 13.4.1 ListBoxes 13.4.2 CheckedListBoxes 13.5 ComboBox es 13.6 TreeView 13.7 ListView s 13.8 Tab Control 13.9 Multiple Document Interface (MDI) Windows 13.10 Visual Inheritance 13.11 User-Defined Controls

2  2002 Prentice Hall. All rights reserved. 2 13.1Introduction Menu –Presents several logically organized options LinkLabel –Link to several destinations ListBox –Manipulate a list of values ComboBox –Drop-down lists TreeView –Display data hierarchically

3  2002 Prentice Hall. All rights reserved. 3 13.2 Menus Menu –Groups related commands –Organize without “cluttering” GUI Menu items –Commands or options in menu Sub-menu –Menu within a menu Hot keys –Alt key shortcuts Press Alt + underlined letter in desired menu item

4  2002 Prentice Hall. All rights reserved. 4 13.2 Menus

5  2002 Prentice Hall. All rights reserved. 5 13.2 Menus

6  2002 Prentice Hall. All rights reserved. 6 13.2 Menus Separator bar: -- right click mouse on the menuitem and select “insert separator” -- typing “-” for the menu text

7  2002 Prentice Hall. All rights reserved. 7 13.2 Menus

8  2002 Prentice Hall. All rights reserved. Outline 8 MenuTest.vb 1 ' Fig 13.5: MenuTest.vb 2 ' Using menus to change font colors and styles. 3 4 Imports System.Windows.Forms 5 6 Public Class FrmMenu 7 Inherits Form 8 9 ' display label 10 Friend WithEvents lblDisplay As Label 11 12 ' main menu (contains file and format menus) 13 Friend WithEvents mnuMainMenu As MainMenu 14 15 ' file menu 16 Friend WithEvents mnuFile As MenuItem 17 Friend WithEvents mnuitmAbout As MenuItem 18 Friend WithEvents mnuitmExit As MenuItem 19 20 ' format menu (contains format and font submenus) 21 Friend WithEvents mnuFormat As MenuItem 22 23 ' color submenu 24 Friend WithEvents mnuitmColor As MenuItem 25 Friend WithEvents mnuitmBlack As MenuItem 26 Friend WithEvents mnuitmBlue As MenuItem 27 Friend WithEvents mnuitmRed As MenuItem 28 Friend WithEvents mnuitmGreen As MenuItem 29

9  2002 Prentice Hall. All rights reserved. Outline 9 MenuTest.vb 30 ' font submenu 31 Friend WithEvents mnuitmFont As MenuItem 32 Friend WithEvents mnuitmTimes As MenuItem 33 Friend WithEvents mnuitmCourier As MenuItem 34 Friend WithEvents mnuitmComic As MenuItem 35 Friend WithEvents mnuitmDash As MenuItem 36 Friend WithEvents mnuitmBold As MenuItem 37 Friend WithEvents mnuitmItalic As MenuItem 38 39 ' Visual Studio.NET generated code 40 41 ' display MessageBox 42 Private Sub mnuitmAbout_Click( _ 43 ByVal sender As System.Object, _ 44 ByVal e As System.EventArgs) Handles mnuitmAbout.Click 45 46 MessageBox.Show("This is an example" & vbCrLf & _ 47 "of using menus.", "About", MessageBoxButtons.OK, _ 48 MessageBoxIcon.Information) 49 End Sub ' mnuitmAbout_Click 50 51 ' exit program 52 Private Sub mnuitmExit_Click( _ 53 ByVal sender As System.Object, _ 54 ByVal e As System.EventArgs) Handles mnuitmExit.Click 55 56 Application.Exit() 57 End Sub ' mnuitmExit_Click 58 This event handler terminates the application when the exit menu item in the file menu is clicked This event handler displays a message box when the about menu item in the file menu is clicked

10  2002 Prentice Hall. All rights reserved. Outline 10 MenuTest.vb 59 ' reset font color 60 Private Sub ClearColor() 61 62 ' clear all checkmarks 63 mnuitmBlack.Checked = False 64 mnuitmBlue.Checked = False 65 mnuitmRed.Checked = False 66 mnuitmGreen.Checked = False 67 End Sub ' ClearColor 68 69 ' update menu state and color display black 70 Private Sub mnuitmBlack_Click(ByVal sender As System.Object, _ 71 ByVal e As System.EventArgs) Handles mnuitmBlack.Click 72 73 ' reset checkmarks for color menu items 74 ClearColor() 75 76 ' set color to black 77 lblDisplay.ForeColor = Color.Black 78 mnuitmBlack.Checked = True 79 End Sub ' mnuitmBlack_Click 80 81 ' update menu state and color display blue 82 Private Sub mnuitmBlue_Click(ByVal sender As System.Object, _ 83 ByVal e As System.EventArgs) Handles mnuitmBlue.Click 84 85 ' reset checkmarks for color menu items 86 ClearColor() 87 88 ' set color to blue 89 lblDisplay.ForeColor = Color.Blue 90 mnuitmBlue.Checked = True 91 End Sub ' mnuitmBlue_Click 92 Each color menu item must be mutually exclusive, so each event handler calls method ClearColor

11  2002 Prentice Hall. All rights reserved. Outline 11 MenuTest.vb 93 ' update menu state and color display red 94 Private Sub mnuitmRed_Click(ByVal sender As System.Object, _ 95 ByVal e As System.EventArgs) Handles mnuitmRed.Click 96 97 ' reset checkmarks for color menu items 98 ClearColor() 99 100 ' set color to red 101 lblDisplay.ForeColor = Color.Red 102 mnuitmRed.Checked = True 103 End Sub ' mnuitmRed_Click 104 105 ' update menu state and color display green 106 Private Sub mnuitmGreen_Click(ByVal sender As System.Object, _ 107 ByVal e As System.EventArgs) Handles mnuitmGreen.Click 108 109 ' reset checkmarks for color menu items 110 ClearColor() 111 112 ' set color to green 113 lblDisplay.ForeColor = Color.Green 114 mnuitmGreen.Checked = True 115 End Sub ' mnuitmGreen_Click 116 117 ' reset font type 118 Private Sub ClearFont() 119 120 ' clear all checkmarks 121 mnuitmTimes.Checked = False 122 mnuitmCourier.Checked = False 123 mnuitmComic.Checked = False 124 End Sub ' ClearFont 125 Each font menu item must be mutually exclusive, so each event handler calls method ClearFont

12  2002 Prentice Hall. All rights reserved. Outline 12 MenuTest.vb 126 ' update menu state and set font to Times 127 Private Sub mnuitmTimes_Click(ByVal sender As System.Object, _ 128 ByVal e As System.EventArgs) Handles mnuitmTimes.Click 129 130 ' reset checkmarks for font menu items 131 ClearFont() 132 133 ' set Times New Roman font 134 mnuitmTimes.Checked = True 135 lblDisplay.Font = New Font("Times New Roman", 30, _ 136 lblDisplay.Font.Style) 137 End Sub ' mnuitmTimes_Click 138 139 ' update menu state and set font to Courier 140 Private Sub mnuitmCourier_Click(ByVal sender As System.Object, _ 141 ByVal e As System.EventArgs) Handles mnuitmCourier.Click 142 143 ' reset checkmarks for font menu items 144 ClearFont() 145 146 ' set Courier font 147 mnuitmCourier.Checked = True 148 lblDisplay.Font = New Font("Courier New", 30, _ 149 lblDisplay.Font.Style) 150 End Sub ' mnuitmCourier_Click 151 152 ' update menu state and set font to Comic Sans MS 153 Private Sub mnuitmComic_Click(ByVal sender As System.Object, _ 154 ByVal e As System.EventArgs) Handles mnuitmComic.Click 155 156 ' reset check marks for font menu items 157 ClearFont() 158

13  2002 Prentice Hall. All rights reserved. Outline 13 MenuTest.vb 159 ' set Comic Sans font 160 mnuitmComic.Checked = True 161 lblDisplay.Font = New Font("Comic Sans MS", 30, _ 162 lblDisplay.Font.Style) 163 End Sub ' mnuitmComic_Click 164 165 ' toggle checkmark and toggle bold style 166 Private Sub mnuitmBold_Click( _ 167 ByVal sender As System.Object, _ 168 ByVal e As System.EventArgs) Handles mnuitmBold.Click 169 170 ' toggle checkmark 171 mnuitmBold.Checked = Not mnuitmBold.Checked 172 173 ' use Xor to toggle bold, keep all other styles 174 lblDisplay.Font = New Font( _ 175 lblDisplay.Font.FontFamily, 30, _ 176 lblDisplay.Font.Style Xor FontStyle.Bold) 177 End Sub ' mnuitmBold_Click 178 179 ' toggle checkmark and toggle italic style 180 Private Sub mnuitmItalic_Click( _ 181 ByVal sender As System.Object, _ 182 ByVal e As System.EventArgs) Handles mnuitmItalic.Click 183 184 ' toggle checkmark 185 mnuitmItalic.Checked = Not mnuitmItalic.Checked 186 187 ' use Xor to toggle italic, keep all other styles 188 lblDisplay.Font = New Font( _ 189 lblDisplay.Font.FontFamily, 30, _ 190 lblDisplay.Font.Style Xor FontStyle.Italic) 191 End Sub ' mnuitmItalic_Click 192 193 End Class ' FrmMenu

14  2002 Prentice Hall. All rights reserved. 14 13.2 Menus

15  2002 Prentice Hall. All rights reserved. 15 13.3 LinkLabels LinkLabel –Displays links to other resources Files Web pages –Behavior similar to web page hyperlink Can change color –New –Previously visited

16  2002 Prentice Hall. All rights reserved. 16 13.3 LinkLabels

17  2002 Prentice Hall. All rights reserved. 17 13.3 LinkLabels

18  2002 Prentice Hall. All rights reserved. Outline 18 1 ' Fig. 13.8: LinkLabelTest.vb 2 ' Using LinkLabels to create hyperlinks. 3 4 Imports System.Windows.Forms 5 6 Public Class FrmLinkLabel 7 Inherits Form 8 9 ' linklabels to C:\ drive, www.deitel.com and Notepad 10 Friend WithEvents lnklblCDrive As LinkLabel 11 Friend WithEvents lnklblDeitel As LinkLabel 12 Friend WithEvents lnklblNotepad As LinkLabel 13 14 ' Visual Studio.NET generated code 15 16 ' browse C:\ drive 17 Private Sub lnklblCDrive_LinkClicked( _ 18 ByVal sender As System.Object, ByVal e As _ 19 System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ 20 Handles lnklblCDrive.LinkClicked 21 22 lnklblCDrive.LinkVisited = True 23 System.Diagnostics.Process.Start("C:\") 24 End Sub ' lnklblCDrive 25 26 ' load www.deitel.com in Web browser 27 Private Sub lnklblDeitel_LinkClicked( _ 28 ByVal sender As System.Object, ByVal e As _ 29 System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ 30 Handles lnklblDeitel.LinkClicked 31 32 lnklblDeitel.LinkVisited = True 33 System.Diagnostics.Process.Start( _ 34 "IExplore", "http://www.deitel.com") 35 End Sub ' lnklblDeitel Event handlers call method Start allowing execution of other programs from our application In this event handler method Start takes two arguments

19  2002 Prentice Hall. All rights reserved. Outline 19 36 37 ' run application Notepad 38 Private Sub lnklblNotepad_LinkClicked( _ 39 ByVal sender As System.Object, ByVal e As _ 40 System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ 41 Handles lnklblNotepad.LinkClicked 42 43 lnklblNotepad.LinkVisited = True 44 45 ' run notepad application 46 ' full path not needed 47 System.Diagnostics.Process.Start("notepad") 48 End Sub ' lnklblNotepad_LinkClicked 49 50 End Class ' LinkLabelList

20  2002 Prentice Hall. All rights reserved. 20 13.3 LinkLabels

21  2002 Prentice Hall. All rights reserved. 21 13.3 LinkLabels

22  2002 Prentice Hall. All rights reserved. 22 13.3 LinkLabels

23  2002 Prentice Hall. All rights reserved. 23 13.4 ListBoxes and CheckedListBoxes ListBox –View and select from multiple items –Scroll bar appears if necessary CheckedListBox –Items have checkbox –Select multiple items at same time –Scroll bar appears if necessary

24  2002 Prentice Hall. All rights reserved. 24 13.4 ListBoxes and CheckedListBoxes

25  2002 Prentice Hall. All rights reserved. 25 13.4.1 ListBoxes

26  2002 Prentice Hall. All rights reserved. 26 13.4.1 ListBoxes

27  2002 Prentice Hall. All rights reserved. Outline 27 ListBoxTest.vb 1 ' Fig. 13.12: ListBoxTest.vb 2 ' Program to add, remove and clear list box items. 3 4 Imports System.Windows.Forms 5 6 Public Class FrmListBox 7 Inherits Form 8 9 ' contains user-input list of elements 10 Friend WithEvents lstDisplay As ListBox 11 12 ' user-input textbox 13 Friend WithEvents txtInput As TextBox 14 15 ' add, remove, clear and exit command buttons 16 Friend WithEvents cmdAdd As Button 17 Friend WithEvents cmdRemove As Button 18 Friend WithEvents cmdClear As Button 19 Friend WithEvents cmdExit As Button 20 21 ' Visual Studio.NET generated code 22 23 ' add new item (text from input box) and clear input box 24 Private Sub cmdAdd_Click(ByVal sender As System.Object, _ 25 ByVal e As System.EventArgs) Handles cmdAdd.Click 26 27 lstDisplay.Items.Add(txtInput.Text) 28 txtInput.Text = "" 29 End Sub ' cmdAdd_Click 30 31 ' remove item if one is selected 32 Private Sub cmdRemove_Click (ByVal sender As System.Object, _ 33 ByVal e As System.EventArgs) Handles cmdRemove.Click 34 The cmdAdd_Click event handler calls method add, which takes the text from the text box as a String

28  2002 Prentice Hall. All rights reserved. Outline 28 ListBoxTest.vb 35 ' remove only if item is selected 36 If lstDisplay.SelectedIndex <> -1 Then 37 lstDisplay.Items.RemoveAt(lstDisplay.SelectedIndex) 38 End If 39 40 End Sub ' cmdRemove_Click 41 42 ' clear all items 43 Private Sub cmdClear_Click (ByVal sender As System.Object, _ 44 ByVal e As System.EventArgs) Handles cmdClear.Click 45 46 lstDisplay.Items.Clear() 47 End Sub ' cmdClear_Click 48 49 ' exit application 50 Private Sub cmdExit_Click (ByVal sender As System.Object, _ 51 ByVal e As System.EventArgs) Handles cmdExit.Click 52 53 Application.Exit() 54 End Sub ' cmdExit_Click 55 56 End Class ' FrmListBox

29  2002 Prentice Hall. All rights reserved. 29 13.4.1 ListBoxes

30  2002 Prentice Hall. All rights reserved. 30 13.4.1 ListBoxes

31  2002 Prentice Hall. All rights reserved. 31 13.4.2 CheckedListBoxes

32  2002 Prentice Hall. All rights reserved. Outline 32 CheckedListBoxTest.v b 1 ' Fig. 13.14: CheckedListBoxTest.vb 2 ' Using the checked list boxes to add items to a list box. 3 4 Imports System.Windows.Forms 5 6 Public Class FrmCheckedListBox 7 Inherits Form 8 9 ' list of available book titles 10 Friend WithEvents chklstInput As CheckedListBox 11 12 ' user selection list 13 Friend WithEvents lstDisplay As ListBox 14 15 ' Visual Studio.NET generated code 16 17 ' item about to change, add or remove from lstDisplay 18 Private Sub chklstInput_ItemCheck( _ 19 ByVal sender As System.Object, _ 20 ByVal e As System.Windows.Forms.ItemCheckEventArgs) _ 21 Handles chklstInput.ItemCheck 22 23 ' obtain reference of selected item 24 Dim item As String = chklstInput.SelectedItem 25 26 ' if item checked add to listbox 27 ' otherwise remove from listbox 28 If e.NewValue = CheckState.Checked Then 29 lstDisplay.Items.Add(item) 30 Else 31 lstDisplay.Items.Remove(item) 32 End If 33 34 End Sub ' chklstInput_ItemCheck An If/Else control structure determines whether the user checked or unchecked an item in the CheckedListBox

33  2002 Prentice Hall. All rights reserved. Outline 33 CheckedListBoxTest.v b 35 36 End Class ' FrmCheckedListBox

34  2002 Prentice Hall. All rights reserved. 34 13.5 ComboBoxes ComboBox –Combines TextBox features with drop-down list –Drop-down list Contains a list from which a value can be selected Scrollbar appears if necessary

35  2002 Prentice Hall. All rights reserved. 35 13.5 ComboBoxes

36  2002 Prentice Hall. All rights reserved. 36 13.5 ComboBoxes

37  2002 Prentice Hall. All rights reserved. Outline 37 1 ' Fig. 13.17: ComboBoxTest.vb 2 ' Using ComboBox to select shape to draw. 3 4 Imports System.Windows.Forms 5 Imports System.Drawing 6 7 Public Class FrmComboBox 8 Inherits Form 9 10 ' contains shape list (circle, square, ellipse, pie) 11 Friend WithEvents cboImage As ComboBox 12 13 ' Visual Studio.NET generated code 14 15 ' get selected index, draw shape 16 Private Sub cboImage_SelectedIndexChanged( _ 17 ByVal sender As System.Object, _ 18 ByVal e As System.EventArgs) _ 19 Handles cboImage.SelectedIndexChanged 20 21 ' create graphics object, pen and brush 22 Dim myGraphics As Graphics = MyBase.CreateGraphics() 23 24 ' create Pen using color DarkRed 25 Dim myPen As New Pen(Color.DarkRed) 26 27 ' create SolidBrush using color DarkRed 28 Dim mySolidBrush As New SolidBrush(Color.DarkRed) 29 30 ' clear drawing area by setting it to color White 31 myGraphics.Clear(Color.White) 32 33 ' find index, draw proper shape 34 Select Case cboImage.SelectedIndex 35 Event handler cboImage_SelectedIndexChange handles the SelectedIndexChange events generated when a user selects an item from the combo box A select case structure is used to determine what item was selected

38  2002 Prentice Hall. All rights reserved. Outline 38 36 Case 0 ' case circle is selected 37 myGraphics.DrawEllipse(myPen, 50, 50, 150, 150) 38 39 Case 1 ' case rectangle is selected 40 myGraphics.DrawRectangle(myPen, 50, 50, 150, 150) 41 42 Case 2 ' case ellipse is selected 43 myGraphics.DrawEllipse(myPen, 50, 85, 150, 115) 44 45 Case 3 ' case pie is selected 46 myGraphics.DrawPie(myPen, 50, 50, 150, 150, 0, 45) 47 48 Case 4 ' case filled circle is selected 49 myGraphics.FillEllipse( _ 50 mySolidBrush, 50, 50, 150, 150) 51 52 Case 5 ' case filled rectangle is selected 53 myGraphics.FillRectangle( _ 54 mySolidBrush, 50, 50, 150, 150) 55 56 Case 6 ' case filled ellipse is selected 57 myGraphics.FillEllipse( _ 58 mySolidBrush, 50, 85, 150, 115) 59 60 Case 7 ' case filled pie is selected 61 myGraphics.FillPie( _ 62 mySolidBrush, 50, 50, 150, 150, 0, 45) 63 64 End Select 65 66 End Sub ' cboImage_SelectedIndexChanged 67 68 End Class ' FrmComboBox

39  2002 Prentice Hall. All rights reserved. 39 13.5 ComboBoxes

40  2002 Prentice Hall. All rights reserved. 40 13.5 ComboBoxes

41  2002 Prentice Hall. All rights reserved. 41 13.6 TreeView TreeView Control –Displays nodes hierarchically –Nodes Objects that contain values –Parent node Contains child nodes Can be expanded or collapsed –Root node First parent node of a tree –Sibling nodes Have same parent node –Child nodes Can have child nodes of their own

42  2002 Prentice Hall. All rights reserved. 42 13.6 TreeView

43  2002 Prentice Hall. All rights reserved. 43 13.6 TreeView

44  2002 Prentice Hall. All rights reserved. 44 13.6 TreeView

45  2002 Prentice Hall. All rights reserved. 45 13.6 TreeView

46  2002 Prentice Hall. All rights reserved. Outline 46 TreeViewDirectoryStr uctureTest.vb 1 ' Fig. 13.22: TreeViewDirectoryStructureTest.vb 2 ' Using TreeView to display directory structure. 3 4 Imports System.Windows.Forms 5 Imports System.IO 6 7 Public Class FrmTreeViewDirectory 8 Inherits Form 9 10 ' contains view of c:\ drive directory structure 11 Friend WithEvents treDirectory As TreeView 12 13 ' Visual Studio.NET generated code 14 15 ' add all subfolders of 'directoryValue' to 'parentNode' 16 Private Sub PopulateTreeView(ByVal directoryValue As String, _ 17 ByVal parentNode As TreeNode) 18 19 ' populate current node with subdirectories 20 Try 21 22 ' get all subfolders 23 Dim directoryArray As String() = _ 24 Directory.GetDirectories(directoryValue) 25 26 If directoryArray.Length <> 0 Then ' if at least one 27 28 Dim currentDirectory As String 29 30 ' for every subdirectory, create new TreeNode, 31 ' add as child of current node and 32 ' recursively populate child nodes with subdirectories 33 For Each currentDirectory In directoryArray 34

47  2002 Prentice Hall. All rights reserved. Outline 47 35 ' create TreeNode for current directory 36 Dim myNode As TreeNode = _ 37 New TreeNode(currentDirectory) 38 39 ' add current directory node to parent node 40 parentNode.Nodes.Add(myNode) 41 42 ' recursively populate every subdirectory 43 PopulateTreeView(currentDirectory, myNode) 44 Next 45 46 End If 47 48 ' catch exception 49 Catch unauthorized As UnauthorizedAccessException 50 parentNode.Nodes.Add("Access Denied") 51 End Try 52 53 End Sub ' PopulateTreeView 54 55 ' called by system when form loads 56 Private Sub FrmTreeViewDirectory_Load(ByVal sender As Object, _ 57 ByVal e As System.EventArgs) Handles MyBase.Load 58 59 ' add c:\ drive to treDirectory and insert its subfolders 60 treDirectory.Nodes.Add("C:") 61 PopulateTreeView("C:\", treDirectory.Nodes(0)) 62 End Sub ' FrmTreeViewDirectory_Load 63 64 End Class ' FrmTreeViewDirectory Adds a root node to the TreeView called treDirectory.C: Method PopulateTreeView takes a directory and a parent node

48  2002 Prentice Hall. All rights reserved. 48 13.6 TreeView

49  2002 Prentice Hall. All rights reserved. 49 13.6 TreeView

50  2002 Prentice Hall. All rights reserved. 50 13.7 ListViews ListView Control –Similar to ListBox –View and select from multiple items –Select multiple items at same time –Displays icons along side of list items

51  2002 Prentice Hall. All rights reserved. 51 13.7 ListViews

52  2002 Prentice Hall. All rights reserved. 52 13.7 ListViews

53  2002 Prentice Hall. All rights reserved. Outline 53 ListViewTest.vb 1 ' Fig. 13.25: ListViewTest.vb 2 ' Displaying directories and their contents in ListView. 3 4 Imports System.Windows.Forms 5 Imports System.IO 6 7 Public Class FrmListView 8 Inherits Form 9 10 ' display labels for current location in directory tree 11 Friend WithEvents lblCurrent As Label 12 Friend WithEvents lblDisplay As Label 13 14 ' displays contents of current directory 15 Friend WithEvents lvwBrowser As ListView 16 17 ' specifies images for file icons and folder icons 18 Friend WithEvents ilsFileFolder As ImageList 19 20 ' Visual Studio.NET generated code 21 22 ' get current directory 23 Dim currentDirectory As String = _ 24 Directory.GetCurrentDirectory() 25 26 ' browse directory user clicked or go up one level 27 Private Sub lvwBrowser_Click(ByVal sender As System.Object, _ 28 ByVal e As System.EventArgs) Handles lvwBrowser.Click 29 30 ' ensure item selected 31 If lvwBrowser.SelectedItems.Count <> 0 Then 32 33 ' if first item selected, go up one level 34 If lvwBrowser.Items(0).Selected Then 35 If SelectedItems.Count = 0 then an item could not have been selected

54  2002 Prentice Hall. All rights reserved. Outline 54 ListViewTest.vb 36 ' create DirectoryInfo object for directory 37 Dim directoryObject As DirectoryInfo = _ 38 New DirectoryInfo(currentDirectory) 39 40 ' if directory has parent, load it 41 If Not (directoryObject.Parent Is Nothing) Then 42 LoadFilesInDirectory( _ 43 directoryObject.Parent.FullName) 44 End If 45 46 ' selected directory or file 47 Else 48 49 ' directory or file chosen 50 Dim chosen As String = _ 51 lvwBrowser.SelectedItems(0).Text 52 53 ' if item selected is directory 54 If Directory.Exists(currentDirectory & _ 55 "\" & chosen) Then 56 57 ' load subdirectory 58 ' if in c:\, do not need "\", otherwise we do 59 If currentDirectory = "C:\" Then 60 LoadFilesInDirectory(currentDirectory & chosen) 61 Else 62 LoadFilesInDirectory(currentDirectory & _ 63 "\" & chosen) 64 End If 65 66 End If 67 68 End If 69 If the selected directory has a parent directory then load it If the first item was not selected then a directory or file must have been chosen Method Exists returns True if its String parameter is a directory

55  2002 Prentice Hall. All rights reserved. Outline 55 ListViewTest.vb 70 ' update lblDisplay 71 lblDisplay.Text = currentDirectory 72 End If 73 74 End Sub ' lvwBrowser_Click 75 76 ' display files/subdirectories of current directory 77 Public Sub LoadFilesInDirectory( _ 78 ByVal currentDirectoryValue As String) 79 80 ' load directory information and display 81 Try 82 83 ' clear ListView and set first item 84 lvwBrowser.Items.Clear() 85 lvwBrowser.Items.Add("Go Up One Level") 86 87 ' update current directory 88 currentDirectory = currentDirectoryValue 89 Dim newCurrentDirectory As DirectoryInfo = _ 90 New DirectoryInfo(currentDirectory) 91 92 ' put files and directories into arrays 93 Dim directoryArray As DirectoryInfo() = _ 94 newCurrentDirectory.GetDirectories() 95 96 Dim fileArray As FileInfo() = _ 97 newCurrentDirectory.GetFiles() 98 99 ' add directory names to ListView 100 Dim dir As DirectoryInfo 101 102 For Each dir In directoryArray 103 A For Each loop is used to add directories to the ListView Files and directories are put into arrays

56  2002 Prentice Hall. All rights reserved. Outline 56 ListViewTest.vb 104 ' add directory to listview 105 Dim newDirectoryItem As ListViewItem = _ 106 lvwBrowser.Items.Add(dir.Name) 107 108 ' set directory image 109 newDirectoryItem.ImageIndex = 0 110 Next 111 112 ' add file names to ListView 113 Dim file As FileInfo 114 115 For Each file In fileArray 116 117 ' add file to ListView 118 Dim newFileItem As ListViewItem = _ 119 lvwBrowser.Items.Add(file.Name) 120 121 newFileItem.ImageIndex = 1 ' set file image 122 Next 123 124 ' access denied 125 Catch exception As UnauthorizedAccessException 126 MessageBox.Show("Warning: Some files may " & _ 127 "not be visible due to permission settings", _ 128 "Attention", 0, MessageBoxIcon.Warning) 129 End Try 130 131 End Sub ' LoadFilesInDirectory 132 133 ' handle load event when Form displayed for first time 134 Private Sub FrmListView_Load(ByVal sender As System.Object, _ 135 ByVal e As System.EventArgs) Handles MyBase.Load 136 A For Each loop is used to add file names to the ListView

57  2002 Prentice Hall. All rights reserved. Outline 57 ListViewTest.vb 137 ' set image list 138 Dim folderImage As Image = Image.FromFile _ 139 (currentDirectory & "\images\folder.bmp") 140 141 Dim fileImage As Image = Image.FromFile _ 142 (currentDirectory & "\images\file.bmp") 143 144 ilsFileFolder.Images.Add(folderImage) 145 ilsFileFolder.Images.Add(fileImage) 146 147 ' load current directory into browserListView 148 LoadFilesInDirectory(currentDirectory) 149 lblDisplay.Text = currentDirectory 150 End Sub ' FrmListView_Load 151 152 End Class ' FrmListView

58  2002 Prentice Hall. All rights reserved. 58 13.7 ListViews

59  2002 Prentice Hall. All rights reserved. 59 13.8 Tab Control TabControl –Creates tabbed windows –Saves space using TabPage objects TabPage objects –Similar to Panels and GroupBoxes –Can contain controls

60  2002 Prentice Hall. All rights reserved. 60 13.8 Tab Control

61  2002 Prentice Hall. All rights reserved. 61 13.8 Tab Control

62  2002 Prentice Hall. All rights reserved. 62 13.8 Tab Control

63  2002 Prentice Hall. All rights reserved. 63 13.8 Tab Control

64  2002 Prentice Hall. All rights reserved. Outline 64 1 ' Fig. 13.30: UsingTabs.vb 2 ' Using TabControl to display various font settings. 3 4 Imports System.Windows.Forms 5 6 Public Class FrmTabs 7 Inherits Form 8 9 ' output label reflects text changes 10 Friend WithEvents lblDisplay As Label 11 12 ' table control containing table pages tbpColor, 13 ' tbpSize, tbpMessage and tbpAbout 14 Friend WithEvents tbcTextOptions As TabControl 15 16 ' table page containing color options 17 Friend WithEvents tbpColor As TabPage 18 Friend WithEvents radBlack As RadioButton 19 Friend WithEvents radRed As RadioButton 20 Friend WithEvents radGreen As RadioButton 21 22 ' table page containing font size options 23 Friend WithEvents tbpSize As TabPage 24 Friend WithEvents radSize12 As RadioButton 25 Friend WithEvents radSize16 As RadioButton 26 Friend WithEvents radSize20 As RadioButton 27 28 ' table page containing text display options 29 Friend WithEvents tbpMessage As TabPage 30 Friend WithEvents radHello As RadioButton 31 Friend WithEvents radGoodbye As RadioButton 32 33 ' table page containing about message 34 Friend WithEvents tbpAbout As TabPage 35 Friend WithEvents lblMessage As Label

65  2002 Prentice Hall. All rights reserved. Outline 65 36 37 ' Visual Studio.NET generated code 38 39 ' event handler for black radio button 40 Private Sub radBlack_CheckedChanged( _ 41 ByVal sender As System.Object, ByVal e As System.EventArgs) _ 42 Handles radBlack.CheckedChanged 43 44 lblDisplay.ForeColor = Color.Black 45 End Sub ' radBlack_CheckedChanged 46 47 ' event handler for red radio button 48 Private Sub radRed_CheckedChanged( _ 49 ByVal sender As System.Object, ByVal e As System.EventArgs) _ 50 Handles radRed.CheckedChanged 51 52 lblDisplay.ForeColor = Color.Red 53 End Sub ' radRed_CheckedChanged 54 55 ' event handler for green radio button 56 Private Sub radGreen_CheckedChanged( _ 57 ByVal sender As System.Object, ByVal e As System.EventArgs) _ 58 Handles radGreen.CheckedChanged 59 60 lblDisplay.ForeColor = Color.Green 61 End Sub ' radGreen_CheckedChanged 62 63 ' event handler for size 12 radio button 64 Private Sub radSize12_CheckedChanged( _ 65 ByVal sender As System.Object, ByVal e As System.EventArgs) _ 66 Handles radSize12.CheckedChanged 67 68 lblDisplay.Font = New Font(lblDisplay.Font.Name, 12) 69 End Sub ' radSize12_CheckedChanged 70

66  2002 Prentice Hall. All rights reserved. Outline 66 71 ' event handler for size 16 radio button 72 Private Sub radSize16_CheckedChanged( _ 73 ByVal sender As System.Object, ByVal e As System.EventArgs) _ 74 Handles radSize16.CheckedChanged 75 76 lblDisplay.Font = New Font(lblDisplay.Font.Name, 16) 77 End Sub ' radSize16_CheckedChanged 78 79 ' event handler for size 20 radio button 80 Private Sub radSize20_CheckedChanged( _ 81 ByVal sender As System.Object, ByVal e As System.EventArgs) _ 82 Handles radSize20.CheckedChanged 83 84 lblDisplay.Font = New Font(lblDisplay.Font.Name, 20) 85 End Sub ' radSize20_CheckedChanged 86 87 ' event handler for message "Hello!" radio button 88 Private Sub radHello_CheckedChanged( _ 89 ByVal sender As System.Object, ByVal e As System.EventArgs) _ 90 Handles radHello.CheckedChanged 91 92 lblDisplay.Text = "Hello!" 93 End Sub ' radHello_CheckedChanged 94 95 ' event handler for message "Goodbye!" radio button 96 Private Sub radGoodbye_CheckedChanged( _ 97 ByVal sender As System.Object, ByVal e As System.EventArgs) _ 98 Handles radGoodbye.CheckedChanged 99 100 lblDisplay.Text = "Goodbye!" 101 End Sub ' radGoodbye_CheckedChanged 102 103 End Class ' FrmTabs

67  2002 Prentice Hall. All rights reserved. 67 13.8 Tab Control

68  2002 Prentice Hall. All rights reserved. 68 13.9 Multiple Document Interface (MDI) Windows MDI –Allows multiple windows Parent window –Application window –Can have many child windows Child window –Cannot be parent –Has exactly one parent –Cannot be moved outside parent –Functionality can be different than other child windows from same parent

69  2002 Prentice Hall. All rights reserved. 69 13.9 Multiple Document Interface (MDI) Windows Single Document Interface (SDI) Multiple Document Interface (MDI)

70  2002 Prentice Hall. All rights reserved. 70 13.9 Multiple Document Interface (MDI) Windows

71  2002 Prentice Hall. All rights reserved. 71 13.9 Multiple Document Interface (MDI) Windows

72  2002 Prentice Hall. All rights reserved. 72 13.9 Multiple Document Interface (MDI) Windows

73  2002 Prentice Hall. All rights reserved. 73 13.9 Multiple Document Interface (MDI) Windows

74  2002 Prentice Hall. All rights reserved. Outline 74 1 ' Fig. 13.37: UsingMDI.vb 2 ' Demonstrating use of MDI parent and child windows. 3 4 Imports System.Windows.Forms 5 6 Public Class FrmUsingMDI 7 Inherits Form 8 9 ' main menu containing menu items File and Window 10 Friend WithEvents mnuMain As MainMenu 11 12 ' menu containing submenu New and menu item Exit 13 Friend WithEvents mnuitmFile As MenuItem 14 Friend WithEvents mnuitmExit As MenuItem 15 16 ' submenu New 17 Friend WithEvents mnuitmNew As MenuItem 18 Friend WithEvents mnuitmChild1 As MenuItem 19 Friend WithEvents mnuitmChild2 As MenuItem 20 Friend WithEvents mnuitmChild3 As MenuItem 21 22 ' menu containing menu items Cascade, TileHorizontal and 23 ' TileVertical 24 Friend WithEvents mnuitmWindow As MenuItem 25 Friend WithEvents mnuitmCascade As MenuItem 26 Friend WithEvents mnuitmTileHorizontal As MenuItem 27 Friend WithEvents mnuitmTileVertical As MenuItem 28 29 ' Visual Studio.NET generated code 30 31 ' create Child1 when menu clicked 32 Private Sub mnuitmChild1_Click( _ 33 ByVal sender As System.Object, _ 34 ByVal e As System.EventArgs) Handles mnuitmChild1.Click 35

75  2002 Prentice Hall. All rights reserved. Outline 75 36 ' create image path 37 Dim imagePath As String = _ 38 Directory.GetCurrentDirectory() & "\images\image0.jpg" 39 40 ' create new child 41 childWindow = New FrmChild(imagePath, "Child1") 42 childWindow.MdiParent = Me ' set parent 43 childWindow.Show() ' display child 44 End Sub ' mnuitmChild1_Click 45 46 ' create Child2 when menu clicked 47 Private Sub mnuitmChild2_Click( _ 48 ByVal sender As System.Object, _ 49 ByVal e As System.EventArgs) Handles mnuitmChild2.Click 50 51 ' create image path 52 Dim imagePath As String = _ 53 Directory.GetCurrentDirectory() & "\images\image1.jpg" 54 55 ' create new child 56 childWindow = New FrmChild(imagePath, "Child2") 57 childWindow.MdiParent = Me ' set parent 58 childWindow.Show() ' display child 59 End Sub ' mnuitmChild2_Click 60 61 ' create Child3 when menu clicked 62 Private Sub mnuitmChild3_Click( _ 63 ByVal sender As System.Object, _ 64 ByVal e As System.EventArgs) Handles mnuitmChild3.Click 65 66 ' create image path 67 Dim imagePath As String = _ 68 Directory.GetCurrentDirectory() & "\images\image2.jpg" 69

76  2002 Prentice Hall. All rights reserved. Outline 76 70 ' create new child 71 childWindow = New FrmChild(imagePath, "Child3") 72 childWindow.MdiParent = Me ' set parent 73 childWindow.Show() ' display child 74 End Sub ' mnuitmChild3_Click 75 76 ' exit application 77 Private Sub mnuitmExit_Click(ByVal sender As System.Object, _ 78 ByVal e As System.EventArgs) Handles mnuitmExit.Click 79 80 Application.Exit() 81 End Sub ' mnuitmExit_Click 82 83 ' set cascade layout 84 Private Sub mnuitmCascade_Click(ByVal sender As System.Object, _ 85 ByVal e As System.EventArgs) Handles mnuitmCascade.Click 86 87 Me.LayoutMdi(MdiLayout.Cascade) 88 End Sub ' mnuitmCascade_Click 89 90 ' set TileHorizontal layout 91 Private Sub mnuitmTileHorizontal_Click( _ 92 ByVal sender As System.Object, ByVal e As System.EventArgs) _ 93 Handles mnuitmTileHorizontal.Click 94 95 Me.LayoutMdi(MdiLayout.TileHorizontal) 96 End Sub ' mnuitmTileHorizontal_Click 97 98 ' set TileVertical layout 99 Private Sub mnuitmTileVertical_Click( _ 100 ByVal sender As System.Object, _ 101 ByVal e As System.EventArgs) Handles mnuitmTileVertical.Click 102 Method LayoutMdi can take values ArrangeIcons, Cascade, TileHorizontal and TileVertical

77  2002 Prentice Hall. All rights reserved. Outline 77 103 Me.LayoutMdi(MdiLayout.TileVertical) 104 End Sub ' mnuitmTileVertical_Click 105 106 End Class ' FrmUsingMDI

78  2002 Prentice Hall. All rights reserved. 78 13.9 Multiple Document Interface (MDI) Windows

79  2002 Prentice Hall. All rights reserved. 79 13.10 Visual Inheritance Allows creation of forms by inheriting from other forms Derived forms contain same functionality as base form including: –Properties –Methods –Variables –Controls –All visual aspects Sizing Layout Spacing Colors and fonts

80  2002 Prentice Hall. All rights reserved. Outline 80 FrmInheritance.vb 1 ' Fig. 13.39: FrmInheritance.vb 2 ' Form template for use with visual inheritance. 3 4 Imports System.Windows.Forms 5 6 Public Class FrmInheritance 7 Inherits Form 8 9 Friend WithEvents lblBug As Label ' top label 10 Friend WithEvents lblCopyright As Label ' bottom label 11 Friend WithEvents cmdLearn As Button ' left button 12 13 ' Visual Studio.NET generated code 14 15 ' invoked when user clicks Learn More button 16 Private Sub cmdLearn_Click(ByVal sender As System.Object, _ 17 ByVal e As System.EventArgs) Handles cmdLearn.Click 18 19 MessageBox.Show("Bugs, Bugs, Bugs is a product of " & _ 20 " Bug2Bug.com.", "Learn More", MessageBoxButtons.OK, _ 21 MessageBoxIcon.Information) 22 End Sub ' cmdLearn_Click 23 24 End Class ' FrmInheritance Method cmdLearn_Click is invoked

81  2002 Prentice Hall. All rights reserved. 81 13.10 Visual Inheritance

82  2002 Prentice Hall. All rights reserved. Outline 82 VisualTest.vb 1 ' Fig. 13.41: VisualTest.vb 2 ' A form that uses visual inheritance. 3 4 Public Class FrmVisualTest 5 Inherits VisualForm.FrmInheritance 6 7 ' new button added to form 8 Friend WithEvents cmdProgram As Button 9 10 ' Visual Studio.NET generated code 11 12 ' invoke when user clicks Learn the Program button 13 Private Sub cmdProgram_Click(ByVal sender As System.Object, _ 14 ByVal e As System.EventArgs) Handles cmdProgram.Click 15 16 MessageBox.Show( _ 17 "This program was created by Deitel & Associates", _ 18 "Learn the Program", MessageBoxButtons.OK, _ 19 MessageBoxIcon.Information) 20 End Sub ' cmdProgram_Click 21 22 End Class ' FrmVisualTest Components, layout and functionality of the base form are inherited by the new form

83  2002 Prentice Hall. All rights reserved. 83 13.10 Visual Inheritance

84  2002 Prentice Hall. All rights reserved. 84 13.11 User-Defined Controls Custom controls –Techniques Inherit from class Control –Define a brand new control Inherit from Windows Forms control –Add functionality to preexisting control Create a UserControl –Multiple preexisting controls

85  2002 Prentice Hall. All rights reserved. 85 13.11 User-Defined Controls

86  2002 Prentice Hall. All rights reserved. Outline 86 CClockUserControl.v b 1 ' Fig 13.43: CClockUserControl.vb 2 ' User-defined control with timer and label. 3 4 Imports System.Windows.Forms 5 6 ' create clock control that inherits from UserControl 7 Public Class CClockUserControl 8 Inherits UserControl 9 10 ' displays time 11 Friend WithEvents lblDisplay As Label 12 13 ' non-visible event-triggering timer object 14 Friend WithEvents tmrClock As Timer 15 16 ' Visual Studio.NET generated code 17 18 ' update label at every tick 19 Private Sub tmrClock_Tick(ByVal sender As System.Object, _ 20 ByVal e As System.EventArgs) Handles tmrClock.Tick 21 22 ' get current time (Now), convert to string 23 lblDisplay.Text = DateTime.Now.ToLongTimeString 24 End Sub ' tmrClock_Tick 25 26 End Class ' CClockUserControl

87  2002 Prentice Hall. All rights reserved. 87 13.11 User-Defined Controls

88  2002 Prentice Hall. All rights reserved. 88 13.11 User-Defined Controls

89  2002 Prentice Hall. All rights reserved. 89 13.11 User-Defined Controls

90  2002 Prentice Hall. All rights reserved. 90 13.11 User-Defined Controls

91  2002 Prentice Hall. All rights reserved. 91 13.11 User-Defined Controls


Download ppt " 2002 Prentice Hall. All rights reserved. 1 Chapter 13 – Graphical User Interfaces: Part 2 Outline 13.1Introduction 13.2 Menus 13.3 LinkLabel s 13.4 ListBox."

Similar presentations


Ads by Google