Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2002 Prentice Hall. All rights reserved. 1 13.6 TreeView TreeView Control –Displays nodes hierarchically –Nodes Objects that contain values –Parent node.

Similar presentations


Presentation on theme: " 2002 Prentice Hall. All rights reserved. 1 13.6 TreeView TreeView Control –Displays nodes hierarchically –Nodes Objects that contain values –Parent node."— Presentation transcript:

1  2002 Prentice Hall. All rights reserved. 1 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

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

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

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

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

6  2002 Prentice Hall. All rights reserved. Outline 6 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

7  2002 Prentice Hall. All rights reserved. Outline 7 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 移除 Node 的 方法為 Remove

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

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

10  2002 Prentice Hall. All rights reserved. 10 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

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

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

13  2002 Prentice Hall. All rights reserved. Outline 13 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

14  2002 Prentice Hall. All rights reserved. Outline 14 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

15  2002 Prentice Hall. All rights reserved. Outline 15 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

16  2002 Prentice Hall. All rights reserved. Outline 16 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

17  2002 Prentice Hall. All rights reserved. Outline 17 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

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

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

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

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

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

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

24  2002 Prentice Hall. All rights reserved. Outline 24 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

25  2002 Prentice Hall. All rights reserved. Outline 25 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

26  2002 Prentice Hall. All rights reserved. Outline 26 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

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

28  2002 Prentice Hall. All rights reserved. 28 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

29  2002 Prentice Hall. All rights reserved. Outline 29 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

30  2002 Prentice Hall. All rights reserved. 30 Compiling into.DLL(1)

31  2002 Prentice Hall. All rights reserved. 31 Compiling into.DLL(2)

32  2002 Prentice Hall. All rights reserved. 32 Add DLL into Project(1) 1. 新增專案 選擇 空專案 2. 專案 -> 繼承表單 顯示 “ 加入新項目 ” 視窗 3. 如下圖,選取 “ 繼承的表單 ” 按 “ 開啟 ” 以顯示 “ 繼承選取器 ” 視窗

33  2002 Prentice Hall. All rights reserved. 33 Add DLL into Project(2) 4. 於 “ 繼承選取器 ” 視窗 按 “ 瀏覽 ” ,選取 VisualForm.dll 5. 如下圖,按 FormInheritance 後下 “ 確定 ” 鈕

34  2002 Prentice Hall. All rights reserved. 34 Add New Program Like Before

35  2002 Prentice Hall. All rights reserved. Outline 35 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

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

37  2002 Prentice Hall. All rights reserved. 37 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

38  2002 Prentice Hall. All rights reserved. 38 Implementation Details 1. 新增專案,選擇 “Windows 控制項程式庫 ”

39  2002 Prentice Hall. All rights reserved. 39 2. 新增如下之控制項及其程式碼 3. 建置 -> 建置方案,會在 方案資料夾底下的 bin 子資料夾產生 WindowsControlLibrary1.dll

40  2002 Prentice Hall. All rights reserved. 40 Using User Defined Controls 1. 新增專案 2. 工具 -> 自訂工具箱

41  2002 Prentice Hall. All rights reserved. 41 3. 於 “ 自訂工具箱 ” 視窗 選擇 “.NET Framework Components” Tab

42  2002 Prentice Hall. All rights reserved. 42 4. 按 “ 瀏覽 ” ,至剛剛子資料夾點選 WindowsControlLibrary1.dll ,按 ” 開啟 ”

43  2002 Prentice Hall. All rights reserved. 43 5. 於 “ 自訂工具箱 ” 視窗 按 “ 確定 ” 即可於 工具箱多了 UserControl1 的控制項, 就可以如一般控制項使用

44  2002 Prentice Hall. All rights reserved. 44 6. 如果希望可以變動 UserControll 裡的控制項,需回到 WindowsControlLibrary1 將那些控制項的存取由 Friend 改 為 Public 改為 Public

45  2002 Prentice Hall. All rights reserved. 45 作業 1.13-31 頁 隨堂練習 2.13-42 頁 實作題 第 4 題 3. 以 TreeView 及 ListView 寫出類似檔案總管的程式 a. 左方之 TreeView 放資料夾,右方之 ListView 放目前被選擇 的資料夾之子資料夾及檔案, b. 左方最上層的資料夾設為 C:\Documents and Settings ( 否則會 執行太久 ) c. 右方之檔案如果為文字檔 (*.txt) ,則點擊兩下可以 NotePad 開 啟該檔,如果為 word 檔 (*.doc) ,則點擊兩下可以 Word 開 啟該檔 d.( 加分, optional) 右方的子資料夾被點擊後,除了會展開右方 該子資料夾,如果左方原子資料之父資料夾還未展開,則左 方也會 Expand 該父資料夾,並將此子資料夾設為 SelectedNode


Download ppt " 2002 Prentice Hall. All rights reserved. 1 13.6 TreeView TreeView Control –Displays nodes hierarchically –Nodes Objects that contain values –Parent node."

Similar presentations


Ads by Google