添加:(先在加一個contextMenu,再它的添加子菜單的click事件編程)
成都創(chuàng)新互聯(lián)專注于網(wǎng)站建設(shè),為客戶提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計開發(fā)服務(wù),多年建網(wǎng)站服務(wù)經(jīng)驗,各類網(wǎng)站都可以開發(fā),成都品牌網(wǎng)站建設(shè),公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設(shè)計,建網(wǎng)站費用,建網(wǎng)站多少錢,價格優(yōu)惠,收費合理。
Try
’使TreeView可以被編輯
TreeView1.LabelEdit = True
‘判斷你是不是選定的是不可編輯的節(jié)點,我這里工種節(jié)點不可以被編輯,只有工種下級的
各個工種名稱可以被編輯
If Trim(TreeView1.SelectedNode.Text) = "工種" Then
‘添加節(jié)點
AddNode = New TreeNode("請輸入新工種名字")
TreeView1.SelectedNode.Nodes.Add(AddNode)
TreeView1.ExpandAll()
AddNode.BeginEdit()
TreeView1.LabelEdit = True
NodeAdded = True
End If
Catch err As Exception
MsgBox(err.ToString)
End Try
刪除與添加類似,只是如果你的節(jié)點名字從其他處(如數(shù)據(jù)庫)得來,那么你還需要更新數(shù)據(jù)庫
編輯:
Private Sub TreeView1_BeforeLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.BeforeLabelEdit
TreeView1.LabelEdit = True ‘使可以編輯
AddNode = TreeView1.SelectedNode
End Sub
Private Sub TreeView1_AfterLabelEdit(ByVal sender As Object, ByVal e As System.windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.AfterLabelEdit
Try
‘此時你改完了節(jié)點名字
TreeView1.SelectedNode.EndEdit(True)
If e.Label Is Nothing Then
'do nothing
ElseIf e.Node.Text = "工種" Then ‘工種不能改
e.CancelEdit() = True
‘e.Node.Text ,e.Label.ToString 一個是改前的名字一個是該后的名字,具體哪個對
哪個請查MSDN
ElseIf Trim(e.Node.Text) "工種" And e.Node.Text e.Label.ToString Then
If MsgBox("此操作會導(dǎo)致當(dāng)前工種中的所有人員的工種都被更改,是否確定?", MsgBoxStyle.YesNo + MsgBoxStyle.Information, "警告") = MsgBoxResult.Yes Then
。。。。 ‘我的更改
MsgBox("更改成功!", MsgBoxStyle.OKOnly, "提示")
'Call InitTree() ‘有時要重新把treeview初始化一遍,視需求定
End If
End If
Catch err As Exception
MsgBox(err.ToString)
End Try
End Sub
其他:
擋treeview得到焦點時你可以使用ContextMenu,反之ContextMenu禁用
Private Sub TreeView1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.GotFocus
TreeView1.ContextMenu = ContextMenu1
End Sub
Private Sub TreeView1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.LostFocus
TreeView1.ContextMenu = Nothing
End Sub
注意:這里沒有在ContextMenu菜單添加“更改”項,而是直接更改:即左鍵單擊節(jié)點表示
選中,再單擊一下就可以編輯了,更改之后單擊他處就完成更改,和你在windows中更改文
件名字相似。
你說的“樹形菜單”是指下拉菜單(可以有子菜單)嗎?如果是,VB.net提供了MenuStrip控件,在工具箱里能找到,直接就在對應(yīng)位置打字就可以了,完全是“所見即所得”。
樹狀結(jié)構(gòu)使用treeview實現(xiàn),控件添加方法:右擊“工具箱”-“部件”-microsoft windows common controls 6.0-確定,應(yīng)用
下面是簡單的添加刪除方法,
Option Explicit
‘添加項目
Private Sub Command1_Click()
Dim nodx As Node
Set nodx = TreeView1.Nodes.Add(, , "zf", "紫風(fēng)無痕關(guān)系(雙擊收起)")
Set nodx = TreeView1.Nodes.Add("zf", tvwChild, "zfwife", "老婆")
Set nodx = TreeView1.Nodes.Add("zf", tvwChild, "zflover", "情人")
Set nodx = TreeView1.Nodes.Add("zf", tvwChild, "zffriend", "朋友")
Set nodx = TreeView1.Nodes.Add("zfwife", tvwChild, "lp1", "大老婆")
Set nodx = TreeView1.Nodes.Add("zfwife", tvwChild, "lp2", "二老婆")
Set nodx = TreeView1.Nodes.Add("zfwife", tvwChild, "lp3", "三老婆")
Set nodx = TreeView1.Nodes.Add("zfwife", tvwChild, "lp4", "四老婆")
Set nodx = TreeView1.Nodes.Add("zfwife", tvwChild, "lp5", "五老婆")
Set nodx = TreeView1.Nodes.Add("zflover", tvwChild, "qr1", "1號情人")
Set nodx = TreeView1.Nodes.Add("zflover", tvwChild, "qr2", "2號情人")
Set nodx = TreeView1.Nodes.Add("zflover", tvwChild, "qr3", "3號情人")
Set nodx = TreeView1.Nodes.Add("zffriend", tvwChild, "py1", "張三")
Set nodx = TreeView1.Nodes.Add("zffriend", tvwChild, "py2", "李四")
Set nodx = TreeView1.Nodes.Add("zffriend", tvwChild, "py3", "王麻子")
Set nodx = TreeView1.Nodes.Add("py1", tvwChild, "pylp1", "張三的老婆")
Set nodx = TreeView1.Nodes.Add("py1", tvwChild, "pylp2", "張三的孩子")
nodx.EnsureVisible
End Sub
'刪除代碼
Private Sub Command2_Click()
MsgBox ("將要刪除" TreeView1.SelectedItem.Text)
TreeView1.Nodes.Remove (TreeView1.SelectedItem.Index)
End Sub