標籤:
方法一: int nodeIndex = 0; bool nodeChanged = false; //右鍵點擊,就進入修改狀態 private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Button == MouseButtons.Right) { e.Node.BeginEdit(); } } private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { nodeIndex = e.Node.Index; nodeChanged = true; e.Node.EndEdit(true ); } private void treeView1_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e) { if (nodeChanged == true) { //在這裡儲存資料到資料庫 MessageBox.Show(treeView1.Nodes[nodeIndex].Text); nodeChanged = false; } }
方法二:
方法二:MouseClick和AfterLabelEdit的使用,treeView1.LabelEdit = true;這句不可缺少:public Form1(){ InitializeComponent(); this.Load+=new EventHandler(Form1_Load); treeView1.MouseClick+=new MouseEventHandler(treeView1_MouseClick); treeView1.AfterLabelEdit+=new NodeLabelEditEventHandler(treeView1_AfterLabelEdit);} private void Form1_Load(object sender, EventArgs e){ for (int i = 0; i < 10; i++) { treeView1.Nodes.Add("tv" + i.ToString()); treeView1.Nodes[i].Nodes.Add("tvc"+i.ToString()); } treeView1.LabelEdit = true;//不可缺少} private void treeView1_MouseClick(object sender, MouseEventArgs e){ TreeView tv=(TreeView)sender;//擷取treeview if (e.Button == MouseButtons.Right)//判斷是否滑鼠右鍵 { if (tv.SelectedNode != null)//判斷是否存在選擇節點 tv.SelectedNode.BeginEdit(); }}private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e){ string newTxt = e.Label;//擷取新文本 //string id = e.Node.Text;//擷取原來的文本 //進行你的更新操作 }
原文地址:http://www.cnblogs.com/better-than-ever/p/3222883.html 感謝原博主。在這裡只是記錄。
C# 運行時編輯 節點重新命名