45
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

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

Embed Size (px)

Citation preview

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

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

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

2002 Prentice Hall. All rights reserved.

2

13.6  TreeView

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

2002 Prentice Hall. All rights reserved.

3

13.6  TreeView

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

2002 Prentice Hall. All rights reserved.

413.6  TreeViewTreeNode properties and methods

Description / Delegate and Event Arguments

Common Properties

Checked Indicates whether the TreeNode is checked (CheckBoxes property must be set to True in parent TreeView).

FirstNode Specifies the first node in the Nodes collection (i.e., first child in tree).

FullPath Indicates the path of the node, starting at the root of the tree.

ImageIndex Specifies the index of the image shown when the node is deselected.

LastNode Specifies the last node in the Nodes collection (i.e., last child in tree).

NextNode Next sibling node.

Nodes The collection of TreeNodes contained in the current node (i.e., all the children of the current node). Contains methods Add (adds a TreeNode object), Clear (deletes the entire collection) and Remove (deletes a specific node). Removing a parent node deletes all its children.

PrevNode Indicates the previous sibling node.

SelectedImageIndex Specifies the index of the image to use when the node is selected.

Text Specifies the TreeView’s text.

Common Methods

Collapse Collapses a node.

Expand Expands a node.

ExpandAll Expands all the children of a node.

GetNodeCount Returns the number of child nodes.

Fig. 13.20 TreeNode properties and methods.

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

2002 Prentice Hall. All rights reserved.

5

13.6  TreeView

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

2002 Prentice Hall.All rights reserved.

Outline6

TreeViewDirectoryStructureTest.vb

1 ' Fig. 13.22: TreeViewDirectoryStructureTest.vb2 ' Using TreeView to display directory structure.3 4 Imports System.Windows.Forms5 Imports System.IO6 7 Public Class FrmTreeViewDirectory8 Inherits Form9 10 ' contains view of c:\ drive directory structure11 Friend WithEvents treDirectory As TreeView12 13 ' Visual Studio .NET generated code14 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 subdirectories20 Try21 22 ' get all subfolders23 Dim directoryArray As String() = _24 Directory.GetDirectories(directoryValue)25 26 If directoryArray.Length <> 0 Then ' if at least one27 28 Dim currentDirectory As String29 30 ' for every subdirectory, create new TreeNode, 31 ' add as child of current node and32 ' recursively populate child nodes with subdirectories33 For Each currentDirectory In directoryArray34

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

2002 Prentice Hall.All rights reserved.

Outline7

35 ' create TreeNode for current directory36 Dim myNode As TreeNode = _37 New TreeNode(currentDirectory)38 39 ' add current directory node to parent node40 parentNode.Nodes.Add(myNode)41 42 ' recursively populate every subdirectory 43 PopulateTreeView(currentDirectory, myNode)44 Next45 46 End If47 48 ' catch exception49 Catch unauthorized As UnauthorizedAccessException50 parentNode.Nodes.Add("Access Denied")51 End Try52 53 End Sub ' PopulateTreeView54 55 ' called by system when form loads56 Private Sub FrmTreeViewDirectory_Load(ByVal sender As Object, _57 ByVal e As System.EventArgs) Handles MyBase.Load58 59 ' add c:\ drive to treDirectory and insert its subfolders60 treDirectory.Nodes.Add("C:")61 PopulateTreeView("C:\", treDirectory.Nodes(0))62 End Sub ' FrmTreeViewDirectory_Load63 64 End Class ' FrmTreeViewDirectory

Adds a root node to theTreeView called treDirectory.C:

Method PopulateTreeViewtakes a directory and a parent node

移除 Node 的方法為 Remove

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

2002 Prentice Hall. All rights reserved.

8

13.6  TreeView

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

2002 Prentice Hall. All rights reserved.

9

13.6  TreeView

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

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

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

2002 Prentice Hall. All rights reserved.

11

13.7  ListViewsListView events and properties

Description / Delegate and Event Arguments

Common Properties

Activation Determines how the user activates an item. This property takes a value in the ItemActivation enumeration. Possible values are OneClick (single-click activation), TwoClick (double-click activation, item changes color when selected) and Standard (double-click activation).

CheckBoxes Indicates whether items appear with checkboxes. True displays checkboxes. False is the default.

LargeImageList Specifies the ImageList containing large icons for display.

Items Returns the collection of ListViewItems in the control.

MultiSelect Determines whether multiple selection is allowed. Default is True, which enables multiple selection.

SelectedItems Lists the collection of selected items.

SmallImageList Specifies the ImageList containing small icons for display.

View Determines appearance of ListViewItems. Values LargeIcon (large icon displayed, items can be in multiple columns), Small-Icon (small icon displayed), List (small icons displayed, items appear in a single column) and Details (like List, but multiple columns of information can be displayed per item).

Common Event (Delegate EventHandler, event arguments EventArgs)

ItemActivate Raised when an item in the ListView is activated. Does not contain the specifics of which item is activated.

Fig. 13.23 ListView properties and events.

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

2002 Prentice Hall. All rights reserved.

12

13.7  ListViews

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

2002 Prentice Hall.All rights reserved.

Outline13

ListViewTest.vb

1 ' Fig. 13.25: ListViewTest.vb2 ' Displaying directories and their contents in ListView.3 4 Imports System.Windows.Forms5 Imports System.IO6 7 Public Class FrmListView8 Inherits Form9 10 ' display labels for current location in directory tree11 Friend WithEvents lblCurrent As Label12 Friend WithEvents lblDisplay As Label13 14 ' displays contents of current directory15 Friend WithEvents lvwBrowser As ListView16 17 ' specifies images for file icons and folder icons18 Friend WithEvents ilsFileFolder As ImageList19 20 ' Visual Studio .NET generated code21 22 ' get current directory23 Dim currentDirectory As String = _24 Directory.GetCurrentDirectory()25 26 ' browse directory user clicked or go up one level27 Private Sub lvwBrowser_Click(ByVal sender As System.Object, _28 ByVal e As System.EventArgs) Handles lvwBrowser.Click29 30 ' ensure item selected31 If lvwBrowser.SelectedItems.Count <> 0 Then32 33 ' if first item selected, go up one level 34 If lvwBrowser.Items(0).Selected Then35

If SelectedItems.Count = 0then an item could not have

been selected

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

2002 Prentice Hall.All rights reserved.

Outline14

ListViewTest.vb

36 ' create DirectoryInfo object for directory37 Dim directoryObject As DirectoryInfo = _38 New DirectoryInfo(currentDirectory)39 40 ' if directory has parent, load it41 If Not (directoryObject.Parent Is Nothing) Then42 LoadFilesInDirectory( _43 directoryObject.Parent.FullName)44 End If45 46 ' selected directory or file47 Else48 49 ' directory or file chosen50 Dim chosen As String = _51 lvwBrowser.SelectedItems(0).Text52 53 ' if item selected is directory 54 If Directory.Exists(currentDirectory & _55 "\" & chosen) Then56 57 ' load subdirectory58 ' if in c:\, do not need "\", otherwise we do59 If currentDirectory = "C:\" Then60 LoadFilesInDirectory(currentDirectory & chosen)61 Else62 LoadFilesInDirectory(currentDirectory & _63 "\" & chosen)64 End If65 66 End If67 68 End If69

If the selected directoryhas a parent directory

then load it

If the first item was not selectedthen a directory or file must have

been chosen

Method Exists returns Trueif its String parameter is a directory

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

2002 Prentice Hall.All rights reserved.

Outline15

ListViewTest.vb

70 ' update lblDisplay71 lblDisplay.Text = currentDirectory72 End If73 74 End Sub ' lvwBrowser_Click75 76 ' display files/subdirectories of current directory77 Public Sub LoadFilesInDirectory( _78 ByVal currentDirectoryValue As String)79 80 ' load directory information and display81 Try82 83 ' clear ListView and set first item84 lvwBrowser.Items.Clear()85 lvwBrowser.Items.Add("Go Up One Level")86 87 ' update current directory88 currentDirectory = currentDirectoryValue89 Dim newCurrentDirectory As DirectoryInfo = _90 New DirectoryInfo(currentDirectory)91 92 ' put files and directories into arrays93 Dim directoryArray As DirectoryInfo() = _94 newCurrentDirectory.GetDirectories()95 96 Dim fileArray As FileInfo() = _97 newCurrentDirectory.GetFiles()98 99 ' add directory names to ListView100 Dim dir As DirectoryInfo101 102 For Each dir In directoryArray103

A For Each loop isused to add directories

to the ListView

Files and directories are putinto arrays

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

2002 Prentice Hall.All rights reserved.

Outline16

ListViewTest.vb

104 ' add directory to listview105 Dim newDirectoryItem As ListViewItem = _106 lvwBrowser.Items.Add(dir.Name)107 108 ' set directory image109 newDirectoryItem.ImageIndex = 0110 Next111 112 ' add file names to ListView113 Dim file As FileInfo114 115 For Each file In fileArray116 117 ' add file to ListView118 Dim newFileItem As ListViewItem = _119 lvwBrowser.Items.Add(file.Name)120 121 newFileItem.ImageIndex = 1 ' set file image122 Next123 124 ' access denied125 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 Try130 131 End Sub ' LoadFilesInDirectory132 133 ' handle load event when Form displayed for first time134 Private Sub FrmListView_Load(ByVal sender As System.Object, _135 ByVal e As System.EventArgs) Handles MyBase.Load136

A For Each loop isused to add file names

to the ListView

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

2002 Prentice Hall.All rights reserved.

Outline17

ListViewTest.vb

137 ' set image list138 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 browserListView148 LoadFilesInDirectory(currentDirectory)149 lblDisplay.Text = currentDirectory150 End Sub ' FrmListView_Load151 152 End Class ' FrmListView

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

2002 Prentice Hall. All rights reserved.

18

13.7  ListViews

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

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

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

2002 Prentice Hall. All rights reserved.

20

13.8  Tab Control

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

2002 Prentice Hall. All rights reserved.

21

13.8  Tab Control

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

2002 Prentice Hall. All rights reserved.

22

13.8  Tab Control

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

2002 Prentice Hall. All rights reserved.

23

13.8  Tab Control

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

2002 Prentice Hall.All rights reserved.

Outline24

1 ' Fig. 13.30: UsingTabs.vb2 ' Using TabControl to display various font settings.3 4 Imports System.Windows.Forms5 6 Public Class FrmTabs7 Inherits Form8 9 ' output label reflects text changes10 Friend WithEvents lblDisplay As Label 11 12 ' table control containing table pages tbpColor,13 ' tbpSize, tbpMessage and tbpAbout14 Friend WithEvents tbcTextOptions As TabControl15 16 ' table page containing color options17 Friend WithEvents tbpColor As TabPage18 Friend WithEvents radBlack As RadioButton19 Friend WithEvents radRed As RadioButton20 Friend WithEvents radGreen As RadioButton21 22 ' table page containing font size options23 Friend WithEvents tbpSize As TabPage24 Friend WithEvents radSize12 As RadioButton25 Friend WithEvents radSize16 As RadioButton26 Friend WithEvents radSize20 As RadioButton27 28 ' table page containing text display options29 Friend WithEvents tbpMessage As TabPage30 Friend WithEvents radHello As RadioButton31 Friend WithEvents radGoodbye As RadioButton32 33 ' table page containing about message34 Friend WithEvents tbpAbout As TabPage35 Friend WithEvents lblMessage As Label

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

2002 Prentice Hall.All rights reserved.

Outline25

36 37 ' Visual Studio .NET generated code38 39 ' event handler for black radio button40 Private Sub radBlack_CheckedChanged( _41 ByVal sender As System.Object, ByVal e As System.EventArgs) _42 Handles radBlack.CheckedChanged43 44 lblDisplay.ForeColor = Color.Black45 End Sub ' radBlack_CheckedChanged46 47 ' event handler for red radio button48 Private Sub radRed_CheckedChanged( _49 ByVal sender As System.Object, ByVal e As System.EventArgs) _50 Handles radRed.CheckedChanged51 52 lblDisplay.ForeColor = Color.Red53 End Sub ' radRed_CheckedChanged54 55 ' event handler for green radio button56 Private Sub radGreen_CheckedChanged( _57 ByVal sender As System.Object, ByVal e As System.EventArgs) _58 Handles radGreen.CheckedChanged59 60 lblDisplay.ForeColor = Color.Green61 End Sub ' radGreen_CheckedChanged62 63 ' event handler for size 12 radio button64 Private Sub radSize12_CheckedChanged( _65 ByVal sender As System.Object, ByVal e As System.EventArgs) _66 Handles radSize12.CheckedChanged67 68 lblDisplay.Font = New Font(lblDisplay.Font.Name, 12)69 End Sub ' radSize12_CheckedChanged70

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

2002 Prentice Hall.All rights reserved.

Outline26

71 ' event handler for size 16 radio button72 Private Sub radSize16_CheckedChanged( _73 ByVal sender As System.Object, ByVal e As System.EventArgs) _74 Handles radSize16.CheckedChanged75 76 lblDisplay.Font = New Font(lblDisplay.Font.Name, 16)77 End Sub ' radSize16_CheckedChanged78 79 ' event handler for size 20 radio button80 Private Sub radSize20_CheckedChanged( _81 ByVal sender As System.Object, ByVal e As System.EventArgs) _82 Handles radSize20.CheckedChanged83 84 lblDisplay.Font = New Font(lblDisplay.Font.Name, 20)85 End Sub ' radSize20_CheckedChanged86 87 ' event handler for message "Hello!" radio button88 Private Sub radHello_CheckedChanged( _89 ByVal sender As System.Object, ByVal e As System.EventArgs) _90 Handles radHello.CheckedChanged91 92 lblDisplay.Text = "Hello!"93 End Sub ' radHello_CheckedChanged94 95 ' event handler for message "Goodbye!" radio button96 Private Sub radGoodbye_CheckedChanged( _97 ByVal sender As System.Object, ByVal e As System.EventArgs) _98 Handles radGoodbye.CheckedChanged99 100 lblDisplay.Text = "Goodbye!"101 End Sub ' radGoodbye_CheckedChanged102 103 End Class ' FrmTabs

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

2002 Prentice Hall. All rights reserved.

27

13.8  Tab Control

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

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

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

2002 Prentice Hall.All rights reserved.

Outline29

FrmInheritance.vb

1    ' Fig. 13.39: FrmInheritance.vb2 ' Form template for use with visual inheritance.3 4 Imports System.Windows.Forms5 6 Public Class FrmInheritance7 Inherits Form8 9 Friend WithEvents lblBug As Label ' top label10 Friend WithEvents lblCopyright As Label ' bottom label11 Friend WithEvents cmdLearn As Button ' left button12 13 ' Visual Studio .NET generated code14 15 ' invoked when user clicks Learn More button16 Private Sub cmdLearn_Click(ByVal sender As System.Object, _17 ByVal e As System.EventArgs) Handles cmdLearn.Click18 19 MessageBox.Show("Bugs, Bugs, Bugs is a product of " & _20 " Bug2Bug.com.", "Learn More", MessageBoxButtons.OK, _21 MessageBoxIcon.Information)22 End Sub ' cmdLearn_Click23 24 End Class ' FrmInheritance

Method cmdLearn_Click is invoked

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

2002 Prentice Hall. All rights reserved.

30

Compiling into .DLL(1)

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

2002 Prentice Hall. All rights reserved.

31

Compiling into .DLL(2)

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

2002 Prentice Hall. All rights reserved.

32

Add DLL into Project(1)

1. 新增專案• 選擇 空專案

2. 專案 -> 繼承表單 顯示 “加入新項目” 視窗

3. 如下圖,選取 “繼承的表單” 按 “開啟” 以顯示 “繼承選取器”視窗

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

2002 Prentice Hall. All rights reserved.

33

Add DLL into Project(2)

4. 於“繼承選取器”視窗 按 “瀏覽”,選取 VisualForm.dll

5. 如下圖,按 FormInheritance 後下 “確定” 鈕

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

2002 Prentice Hall. All rights reserved.

34

Add New Program Like Before

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

2002 Prentice Hall.All rights reserved.

Outline35

VisualTest.vb

1 ' Fig. 13.41: VisualTest.vb2 ' A form that uses visual inheritance.3 4 Public Class FrmVisualTest5 Inherits VisualForm.FrmInheritance6 7 ' new button added to form8 Friend WithEvents cmdProgram As Button 9 10 ' Visual Studio .NET generated code11 12 ' invoke when user clicks Learn the Program button13 Private Sub cmdProgram_Click(ByVal sender As System.Object, _14 ByVal e As System.EventArgs) Handles cmdProgram.Click15 16 MessageBox.Show( _17 "This program was created by Deitel & Associates", _18 "Learn the Program", MessageBoxButtons.OK, _19 MessageBoxIcon.Information)20 End Sub ' cmdProgram_Click21 22 End Class ' FrmVisualTest

Components, layout andfunctionality of the base formare inherited by the new form

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

2002 Prentice Hall. All rights reserved.

36

13.10  Visual Inheritance

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

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

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

2002 Prentice Hall. All rights reserved.

38

Implementation Details

1. 新增專案,選擇 “ Windows 控制項程式庫”

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

2002 Prentice Hall. All rights reserved.

39

2. 新增如下之控制項及其程式碼

3. 建置 -> 建置方案,會在 方案資料夾底下的 bin 子資料夾產生 WindowsControlLibrary1.dll

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

2002 Prentice Hall. All rights reserved.

40

Using User Defined Controls

1. 新增專案2. 工具 -> 自訂工具箱

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

2002 Prentice Hall. All rights reserved.

41

3. 於 “自訂工具箱”視窗 選擇 “ .NET Framework Components” Tab

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

2002 Prentice Hall. All rights reserved.

42

4. 按 “瀏覽”,至剛剛子資料夾點選 WindowsControlLibrary1.dll ,按”開啟”

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

2002 Prentice Hall. All rights reserved.

43

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

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

2002 Prentice Hall. All rights reserved.

44

6. 如果希望可以變動 UserControll 裡的控制項,需回到 WindowsControlLibrary1 將那些控制項的存取由 Friend 改為 Public

改為 Public

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

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