很簡(jiǎn)單,在頁(yè)面上拖個(gè)treeview,可以直接給它實(shí)裝數(shù)據(jù),運(yùn)行一下,就可展開(kāi)。
成都創(chuàng)新互聯(lián)長(zhǎng)期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為巴宜企業(yè)提供專(zhuān)業(yè)的成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì),巴宜網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
你說(shuō)的“樹(shù)形菜單”是指下拉菜單(可以有子菜單)嗎?如果是,VB.net提供了MenuStrip控件,在工具箱里能找到,直接就在對(duì)應(yīng)位置打字就可以了,完全是“所見(jiàn)即所得”。
數(shù)據(jù)庫(kù)中樹(shù)形圖的用法的用法你知道嗎?下面我就跟你們?cè)敿?xì)介紹下數(shù)據(jù)庫(kù)中樹(shù)形圖的用法的用法,希望對(duì)你們有用。
數(shù)據(jù)庫(kù)中樹(shù)形圖的用法的用法如下:
樹(shù)形圖用于顯示按照樹(shù)形結(jié)構(gòu)進(jìn)行組織的數(shù)據(jù),其用途比較廣泛,如計(jì)算機(jī)中的文件系統(tǒng)(Windows中的資源管理器)、企業(yè)或公司的組成結(jié)構(gòu)等。我們知道在Windows下VB、PB、Delphi等工具提供了一個(gè)功能很強(qiáng)的樹(shù)型控件TreeView,利用Treeview控件可以方便地開(kāi)發(fā)樹(shù)形圖。然而在網(wǎng)頁(yè)上實(shí)現(xiàn)樹(shù)形圖就不那么容易了,現(xiàn)在在asp.net中利用微軟提供的Internet Explorer WebControls它使得網(wǎng)頁(yè)上的樹(shù)形圖開(kāi)發(fā)與在Windows下一樣的方便,一樣的功能強(qiáng)大,甚至更靈活。
本文介紹用Internet Explorer WebControls開(kāi)發(fā)樹(shù)形圖的方法,由于樹(shù)形圖結(jié)構(gòu)較復(fù)雜,使用起來(lái)常不知如何下手。筆者結(jié)合最近剛為公司用ASP.NET編寫(xiě)的應(yīng)用程序管理器這一具體實(shí)例,詳細(xì)闡述在ASP.NET下如何將Internet Explorer WebControls的使用與數(shù)據(jù)庫(kù)聯(lián)系起來(lái),實(shí)現(xiàn)數(shù)據(jù)分任意多層顯示,方便地進(jìn)行增加、修改、刪除、移動(dòng)操作。筆者希望通過(guò)對(duì)該實(shí)例的闡述,達(dá)到拋磚引玉的效果,與各位同仁相互交流,共同進(jìn)步。
Internet Explorer WebControls不在VS.NET的標(biāo)準(zhǔn)Server Control中,要到微軟的站點(diǎn)上下載,下載地址是:
下載安裝后第一次使用時(shí),要右擊工具箱Customize Toolbox…→.NET Framework Components中找到Micosoft.Web.UI.WebControls.Treeview后選中,這樣Treeview控件就出現(xiàn)在工具箱中了。
一、樹(shù)的建立
具體方法是:創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),設(shè)計(jì)樹(shù)圖信息表TREE_INFO,包含NODEID、PARENTID、NODENAME、ADDErss、ICON字段,其它字段根據(jù)實(shí)際業(yè)務(wù)而定,節(jié)點(diǎn)名稱(chēng)NODENAME將在樹(shù)型控件的節(jié)點(diǎn)上顯示,NODEID字段保存節(jié)點(diǎn)的唯一標(biāo)識(shí)號(hào),PARENTID表示當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)號(hào),標(biāo)識(shí)號(hào)組成了一個(gè)“鏈表”,記錄了樹(shù)上節(jié)點(diǎn)的結(jié)構(gòu)。設(shè)計(jì)一個(gè)Web窗體其上放置TreeView控件。
PRivate Sub CreateDataSet()’建立數(shù)據(jù)集
Dim myConn As New SqlConnection()
Dim myCmd As New SqlCommand("select NODEID,NODENAME,PARENTID,ADDRESS,ICON from Tree_info", myConn)
Dim myDataAdapter As New SqlDataAdapter()
myConn.ConnectionString = application("connectstring")
myCmd.CommandText = ""
myCmd.Connection = myConn
myDataAdapter.SelectCommand = myCmd
myDataAdapter.Fill(ds, "tree")
End Sub
建樹(shù)的基本思路是:從根節(jié)點(diǎn)開(kāi)始遞歸調(diào)用顯示子樹(shù)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateDataSet()
intiTree(TreeView1.Nodes, 0)
End Sub
Private Sub intiTree(ByRef Nds As TreeNodeCollection, ByVal parentId As Integer)
Dim dv As New DataView()
Dim drv As DataRowView
Dim tmpNd As TreeNode
Dim intId As Integer
dv.Table = ds.Tables("tree")
dv.RowFilter = "PARENTID=’" parentId "’"
For Each drv In dv
tmpNd = New TreeNode()
strId = drv("NODE_ID")
tmpNd.ID = strId
tmpNd.Text = drv("NODE_NAME ")
tmpNd.ImageUrl = drv("ICON").ToString
Nds.Add(tmpNd)
intiTree(Nds(Nds.Count - 1).Nodes, intId)
Next
End Sub
二、增加、刪除樹(shù)節(jié)點(diǎn)
單純?cè)赥reeview 上增加、刪除、修改節(jié)點(diǎn)只需用Nodes屬性的Add、 Remove、等方法即可,值得注意的地方是VS.NET中Treeview的Nodes集合與VS6.0中的區(qū)別,VS6.0中的是一個(gè)大的集合,而VS.NET中的是分層的每個(gè)Node下都有Nodes屬性。增加、刪除、修改樹(shù)節(jié)點(diǎn)時(shí)與VS6.0相比有很大差別,特別是刪除時(shí)。
Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAdd.Click’在選定的節(jié)點(diǎn)下添加子節(jié)點(diǎn)
Dim tmpNd As New TreeNode(), NdSel As TreeNode
tmpNd.ID = GetNewId()
NdSel = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex)’選中的節(jié)點(diǎn)
tmpNd.Text = "新節(jié)點(diǎn)"
NdSel.Nodes.Add(tmpNd)
Dim myRow As DataRow
myRow = ds.Tables("tree").NewRow()
myRow("NODE_NAME") = tmpNd.ID
myRow("NODE_DESCRipT") = "新節(jié)點(diǎn)" tmpNd.ID "_" NdSel.ID
myRow("PARENT_NAME") = NdSel.ID
ds.Tables("tree").Rows.Add(myRow)
End Sub
Private Sub ButDele_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButDele.Click’刪除選中的節(jié)點(diǎn)
Dim idx As String = TreeView1.SelectedNodeIndex()
GetNdCol(idx).Remove(TreeView1.GetNodeFromIndex(idx))
Dim dv As New DataView(), recNo As Integer
dv.Table = ds.Tables("tree")
dv.RowFilter= "NODEID=" NdId
dv.Delete(0)
End Sub
Private Function GetNdCol(ByVal idx As String) As TreeNodeCollection
‘獲得選中節(jié)點(diǎn)的父節(jié)點(diǎn)的Nodes集合
Dim cnt As Integer, i As Integer
Dim tmpNds As TreeNodeCollection
Dim idxs() As String
idxs = Split(idx, ".")
cnt = UBound(idxs)
If cnt = 0 Then
tmpNds = TreeView1.Nodes
Else
tmpNds = TreeView1.Nodes(CInt(idxs(0))).Nodes
For i = 1 To cnt - 1
tmpNds = tmpNds(CInt(idxs(i))).Nodes
Next
End If
Return tmpNds
End Function
三、修改、移動(dòng)樹(shù)節(jié)點(diǎn)
由于服務(wù)器控件不支持鼠標(biāo)拖動(dòng)事件,所以不能象Windows程序那樣通過(guò)拖動(dòng)移動(dòng)節(jié)點(diǎn),這里是通過(guò)選擇父節(jié)點(diǎn)的方式。移動(dòng)是通過(guò)在原位置刪除,新位置添加實(shí)現(xiàn)的,要注意在刪除時(shí)先保存節(jié)點(diǎn)信息。
Private Sub TreeView1_SelectedIndexChange(ByVal sender As Object, ByVal e As Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs) Handles TreeView1.SelectedIndexChange
Dim dv As New DataView()
dv.Table = ds.Tables("tree")
Dim tmpNd As TreeNode = TreeNdSel(e.OldNode), tmpNds As TreeNodeCollection
dv.RowFilter= "NODEID=" tmpNd.ID
dv(0)("NODE_DESCRIPT") = Me.TextBox1.Text
dv(0)("ADDRESS") = Me.TextBox2.Text
dv(0)("TARGET") = Me.TextBox3.Text
dv(0)("ICON") = Me.TextBox4.Text
If dv(0)("PARENTID").ToString Me.DropDownList1.SelectedItem.Value Then
‘移動(dòng)節(jié)點(diǎn)
dv(0)("PARENT_NAME") = Me.DropDownList1.SelectedItem.Value
If Me.DropDownList1.SelectedItem.Value = "ROOT" Then
tmpNds = TreeView1.Nodes
Else
tmpNds = FromIdToNode(Me.DropDownList1.SelectedItem.Value, TreeView1.Nodes).Nodes’新的父節(jié)點(diǎn)的Nodes集合
End If
GetNdCol(e.OldNode).Remove(tmpNd)
tmpNds.Add(tmpNd)
End If
tmpNd.Text = Me.TextBox1.Text
tmpNd.ImageUrl = Me.TextBox4.Text
tmpNd = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex)
dv.RowFilter= "NODEID=" tmpNd.ID
Me.TextBox1.Text = dv(0)("NODENAME").ToString
Me.TextBox2.Text = dv(0)("ADDRESS").ToString
Me.TextBox3.Text = dv(0)("TARGET").ToString
Me.TextBox4.Text = dv(0)("ICON").ToString
End Sub
Private Function FromIdToNode(ByVal ID As String, ByVal Nds As TreeNodeCollection) As TreeNode
‘由關(guān)鍵字查找節(jié)點(diǎn)
Dim i As Integer
Dim tmpNd As TreeNode, tmpNd1 As TreeNode
For Each tmpNd In Nds
If tmpNd.ID = ID Then
Return tmpNd
Exit Function
End If
tmpNd1 = FromIdToNode(ID, tmpNd.Nodes)
If Not (tmpNd1 Is Nothing) Then
Return tmpNd1
Exit Function
End If
Next
Return Nothing
End Function
BOOL WINAPI EnumChildWindows
(v=vs.85).aspx
找對(duì)方主窗口中的子窗口,直到子窗口類(lèi)為T(mén)reeView
然后再用個(gè)CWnd* pOtherTreeWnd = CWnd::Accach(treeView.m_hWnd);
網(wǎng)頁(yè)名稱(chēng):vb.net開(kāi)發(fā)樹(shù)形圖 vb 樹(shù)形控件
文章出自:http://www.rwnh.cn/article4/dosheoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)頁(yè)設(shè)計(jì)公司、定制開(kāi)發(fā)、App開(kāi)發(fā)、Google、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)