燃燒吧~~~TreeView-配合物件導向的設計方式 VB.NET&C# 混合

在之前的文章-->ASP.NET 2.0動態產生TreeView樹狀結構-C# & VB.NET 混合 提到過動態產生Menu

如果和物件導向結合 會是什麼樣的情景呢!?(當然 顯示出來的結果要是一樣,只是內部設計不同)

有關於物件導向設計方式 請參考 -->燃燒吧~~ObjectDataSouce配合物件導向的設計方式連結資料 VB.NET

廢話不多說~實際操作看看吧!

在之前的文章-->ASP.NET 2.0動態產生TreeView樹狀結構-C# & VB.NET 混合 提到過動態產生Menu

如果和物件導向結合 會是什麼樣的情景呢!?(當然 顯示出來的結果要是一樣,只是內部設計不同)

有關於物件導向設計方式 請參考 -->燃燒吧~~ObjectDataSouce配合物件導向的設計方式連結資料 VB.NET

廢話不多說~實際操作看看吧!


首先 設計一個 objMenu的模型出來

01 ''' <summary>
02 ''' Menu 資料庫物件,資料存取層物件。 By Phoehix - 2008
03 ''' </summary>
04 ''' <remarks></remarks>
05 Public Class objMenu
06     Inherits objADU
07     Private _ID As Integer
08     Private _ParentID As Integer
09     Private _Value As String
10     Private _Order As Integer
11     Public Property MenuID() As Integer
12         Get
13             Return _ID
14         End Get
15         Set(ByVal value As Integer)
16             _ID = value
17         End Set
18     End Property
19
20     Public Property MenuParentID() As Integer
21         Get
22             Return _ParentID
23         End Get
24         Set(ByVal value As Integer)
25             _ParentID = value
26         End Set
27     End Property
28
29     Public Property MenuValue() As String
30         Get
31             Return _Value
32         End Get
33         Set(ByVal value As String)
34             _Value = value
35         End Set
36     End Property
37     Public Property MenuOrder() As Integer
38         Get
39             Return _Order
40         End Get
41         Set(ByVal value As Integer)
42             _Order = value
43         End Set
44     End Property
45     Public Sub New()
46
47     End Sub

48     Public Sub New(ByRef row As DataRow)
49         GetMenu(row)
50     End Sub

51     Private Sub GetMenu(ByRef row As DataRow)
52         Try
53             _ID = Integer.Parse(row(0).ToString())
54             _ParentID = Integer.Parse(row(1).ToString())
55             _Value = row(2).ToString()
56             _Order = Integer.Parse(row(3).ToString())
57         Catch ex As Exception
58             Throw
59         End Try
60     End Sub

61     Public Overloads Sub Add()
62         Try
63             Dim SqlTxt As String = "INSERT INTO [Menu] ([ParentID], [Value], [Order]) VALUES (@ParentID, @Value, @Order)"
64             Dim Parameters(3) As SqlParameter
65             Parameters(0) = New SqlParameter("@ParentID", _ParentID)
66             Parameters(1) = New SqlParameter("@Value", _Value)
67             Parameters(3) = New SqlParameter("@Order", _Order)
68             add(SqlTxt, Parameters)
69         Catch ex As Exception
70             Throw
71         End Try
72     End Sub

73     Public Overloads Sub Del()
74         Try
75             Dim SqlTxt As String = "Delete [Menu] WHERE ([ID] = @Original_ID)"
76             Dim Parameter As SqlParameter = New SqlParameter("@Original_ID", _ID)
77             Del(SqlTxt, Parameter)
78         Catch ex As Exception
79             Throw
80         End Try
81     End Sub

82     Public Overloads Sub Update()
83         Try
84             Dim SqlTxt As String = "UPDATE Menu Set ParentID = @ParentID, Value = @Value, Order = @Order WHERE ID = @Original_ID"
85             Dim Parameters(4) As SqlParameter
86             Parameters(0) = New SqlParameter("@ParentID", _ParentID)
87             Parameters(1) = New SqlParameter("@Value", _Value)
88             Parameters(3) = New SqlParameter("@Order", _Order)
89             Parameters(4) = New SqlParameter("@Original_ID", _ID)
90             Update(SqlTxt, Parameters)
91         Catch ex As Exception
92             Throw
93         End Try
94     End Sub

95     Protected Overrides Sub Finalize()
96         MyBase.Finalize()
97     End Sub

98 End Class

 

再來設計一個配合存取資料的BLL.daoMenu,進行新增、修改、刪除、查詢的動作

01 Public Class daoMenu
02     Public Function GetMenu() As List(Of DAL.objMenu)
03         Try
04             Dim rMenu As New List(Of DAL.objMenu)
05             rMenu.Clear()
06             Dim oData As New DAL.objData()
07             Dim SqlTxt As String = "SELECT * FROM Menu ORDER BY [Order]"
08             Dim dt As New DataTable
09             If oData.SqlSelectQuery(SqlTxt, dt) Then
10                 For Each row As DataRow In dt.Rows
11                     rMenu.Add(New DAL.objMenu(row))
12                 Next
13             End If
14             Return rMenu
15         Catch ex As Exception
16             Throw
17         End Try
18     End Function

19     Public Sub MenuUpdate(ByVal oMenu As DAL.objMenu)
20         Try
21             oMenu.Update()
22         Catch ex As Exception
23             Throw
24         End Try
25     End Sub

26     Public Sub MenuDel(ByVal oMenu As DAL.objMenu)
27         Try
28             oMenu.Del()
29         Catch ex As Exception
30             Throw
31         End Try
32     End Sub

33     Public Sub MenuAddNew(ByVal oMenu As DAL.objMenu)
34         Try
35             oMenu.Add()
36         Catch ex As Exception
37             Throw
38         End Try
39     End Sub

40 End Class

再來 關於GetMenu,AddRoot,AddNodes 修改如下

01         /// <summary>
02         /// 取得MenuTree By Phoehix - 2008
03         /// </summary>
04         /// <param name="MenuTree">TreeView</param>
05         /// <returns>TreeView</returns>

06         public bool GetMenu(out TreeView MenuTree)
07         {
08             MenuTree = new TreeView();
09             BLL.daoMenu daoMenu = new BLL.daoMenu();
10             List<DAL.objMenu> objMenu;
11             objMenu = daoMenu.GetMenu();
12             bool result = false;
13             result = AddRoot(ref MenuTree,ref objMenu);
14             if (result)
15             {
16                 MenuTree.ImageSet = TreeViewImageSet.Custom;
17                 MenuTree.NodeIndent = 0;
18                 return true;
19             }

20             else
21             {
22                 return false;
23             }

24         }

9-宣告 daoMenu 成為新的執行個體

10- 宣告 objMenu 為一個物件的List集合

11-從daoMenu.GetMenu() 這個方法填入objMenu

13-把參數 用Ref的方式傳過去

14~ 類似的不再贅述


再來 ~AddRoot

01         private bool AddRoot(ref TreeView MenuTree, ref List<DAL.objMenu> objMenu)
02         {
03             try
04             {
05                 int tmpNodeID;
06                 List<DAL.objMenu> lists = objMenu.FindAll(delegate(DAL.objMenu list) { return list.MenuParentID == 0; });
07                 if (lists.Count >= 0)
08                 {
09                     TreeNode NewNode;
10                     bool rc;
11                     foreach (DAL.objMenu list in lists)
12                     {
13                         NewNode = new TreeNode();
14                         tmpNodeID = list.MenuID;
15                         NewNode.Text = list.MenuValue;
16
17                         MenuTree.Nodes.Add(NewNode);
18
19                         rc = AddNodes(ref NewNode, tmpNodeID, ref objMenu);
20                     }

21                 }

22                 lists = null;
23                 return true;
24             }

25             catch
26             {
27                 return false;
28             }

29         }

修改的部份為

6- 有趣 有趣 ... 先宣告一個lists 來承接 objMenu.FindAll 出來的結果 (厄!? 其實我忘記這是哪邊看來的 ...

這裡重點就是在delegate(DAL.objMenu list) { return list.MenuParentID == 0; } 意思是 傳回 MenuParentID = 0(跟節點)

9-判斷 是否有資料

11-把資料 一一的取出

13-15 把資料填入

19-傳objMenu過去 一樣用ref方式 (應該是可以節省 複製一份出來的記憶體 ...)

22-告訴.NET 我不要lists 了= =、


最後AddNodes

01         private bool AddNodes(ref TreeNode tNode, int PID, ref List<DAL.objMenu> objMenu)
02         {
03             try
04             {
05                 int tmpNodeID;
06                 List<DAL.objMenu> lists = objMenu.FindAll(delegate(DAL.objMenu list) { return list.MenuParentID == PID; });
07                 if (lists.Count >=0)
08                 {
09                     TreeNode NewNode;
10                     bool rc;
11                     foreach (DAL.objMenu list in lists)
12                     {
13                         NewNode = new TreeNode();
14                         tmpNodeID = list.MenuID;
15                         NewNode.Text = list.MenuValue;
16
17                         tNode.ChildNodes.Add(NewNode);
18
19                         rc = AddNodes(ref NewNode, tmpNodeID, ref objMenu);
20                     }

21                 }

22                 lists = null;
23
24                 return true;
25             }

26             catch
27             {
28                 return false;
29             }

30         }

修改的部份如下

6- 跟上面差不多 只是 ParentID 要改用傳過來的節點代號 篩選出該節點的子節點

11-foreach一一取出資料

13~15 填入資料

然後就完成囉!

當然 ... 顯示出來的結果會是一樣的

以上 ~ 我覺得 比較值得一提的是 LIST<T>.FindAll 這個方法 ... 蠻有趣的呢~ 大家可以參考MSDN ->List<(Of <(T>)>)..::.FindAll 方法

Phoenix 8/5