treeview中实现右击弹出式菜单
右击treeview中某个节点的时候,可以弹出一个菜单,菜单中的内容为给改节点的操作,比如:添加子节点,修改,删除,谁能给个源码,谢谢啦,分不够再加哈
[解决办法]
沙发,帮忙顶
[解决办法]
添加:(先在加一个contextMenu,再它的添加子菜单的click事件编程)
Try
’使TreeView可以被编辑
TreeView1.LabelEdit = True
‘判断你是不是选定的是不可编辑的节点,我这里工种节点不可以被编辑,只有工种下级的
各个工种名称可以被编辑
If Trim(TreeView1.SelectedNode.Text) = "工种 " Then
‘添加节点
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
删除与添加类似,只是如果你的节点名字从其他处(如数据库)得来,那么你还需要更新数据库
编辑:
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
‘此时你改完了节点名字
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( "此操作会导致当前工种中的所有人员的工种都被更改,是否确定? ", 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菜单添加“更改”项,而是直接更改:即左键单击节点表示
选中,再单击一下就可以编辑了,更改之后单击他处就完成更改,和你在windows中更改文
件名字相似。
你不能完全照抄代码,需要和你的具体要求作相应的更改。
[解决办法]
也许对你有用
客户端操作TreeView一直是比较头疼的问题,但也比较实用。好好研究了一下TreeView.htc,找到以下方法,不要告诉我你早已知道而懒得说:)
1.设置所选节点,如选中第二个节点
function SetSelNode()
{
TreeView1.selectedNodeIndex= "1 ";
}
2.得到所选节点的Text,ID或NodeData
function GetAttribute()
{
alert(TreeView1.getTreeNode(TreeView1.selectedNodeIndex).getAttribute( "Text "));
}
替换Text为ID或NodeData,可分别得到所选节点的ID或NodeData
3.修改节点属性,如修改第一个节点的Text
function ModifyNode()
{
var node=TreeView1.getTreeNode( "0 ");
node.setAttribute( "Text ", "hgknight ");
}
4.得到点击节点
function TreeView1.onclick()
{
alert(TreeView1.getTreeNode(TreeView1.clickedNodeIndex).getAttribute( "Text "));
}
5.添加节点
function AddNode()
{
var node=TreeView1.createTreeNode();
node.setAttribute( "Text ", "hgknight ");
TreeView1.add(node);
}
--------------------------------
下面的js 实现右键菜单弹出
treeview.js
var menuskin = "skin ";
var node = null;
//Calculate string length, one chinese char impropriate two byte.
String.prototype.lenB = function()
{
return this.replace(/[^\x00-\xff]/g, "** ").length;
}
function hideMenu()
{
popupMenu.style.visibility = "hidden ";
}
function highlighItem()
{
if (event.srcElement.className == "menuitems ")
{
event.srcElement.style.backgroundColor = "highlight ";
event.srcElement.style.color = "white ";
}
}
function lowlightItem()
{
if (event.srcElement.className == "menuitems ")
{
event.srcElement.style.backgroundColor = " ";
event.srcElement.style.color = "black ";
window.status = " ";
}
}
//Call pop menu function
function clickItem()
{
if (event.srcElement.className == "menuitems ")
{
if(event.srcElement.getAttribute( "func ") == "add " && node != null)
{
var strNewNodeName;
strNewNodeName = prompt( '请输入新的目录名字: ', ' ');
if(strNewNodeName.lenB() < 20 & strNewNodeName.lenB() > 0){
window.document.getElementById( "TBNewCatalogName ").value = strNewNodeName;
document.all.Button1.click();
}
else
alert( "目录名长度不对 ");
}
else if (event.srcElement.getAttribute( "func ") == "delete " && node != null)
{
if(confirm( "确定要删除此目录吗? "))
{
if(window.document.getElementById( "TBNodeIndex ").value == "0 ")
alert( "不能删除此目录. ");
else
document.all.Button2.click();
}
}
else if (event.srcElement.getAttribute( "func ") == "modify " && node != null)
{
//User can 't edit root node name
if(window.document.getElementById( "TBNodeIndex ").value != "0 ")
{
var strEditNodeName;
strEditNodeName = prompt( '请输入新的目录名字: ', ' ');
if(strEditNodeName.lenB() < 20 & strEditNodeName.lenB() > 0){
window.document.getElementById( "TBNewCatalogName ").value = strEditNodeName;
document.all.Button3.click();
}
else
alert( "目录名长度不对 ");
}
else
alert( "不能修改根结点 ");
}
}
}
function TreeView1.oncontextmenu()
{
var nodeindex = event.treeNodeIndex;
if (typeof(nodeindex) == "undefined ")
{
node = null;
return;
}
node = TreeView1.getTreeNode(nodeindex);
window.document.getElementById( "TBNodeIndex ").value = nodeindex;
var rightedge = document.body.clientWidth-event.clientX;
var bottomedge = document.body.clientHeight-event.clientY;
if (rightedge <popupMenu.offsetWidth)
{
//old author code
//popupMenu.style.left = document.body.scrollLeft + event.clientX - popupMenu.offsetWidth;
popupMenu.style.left = 30;
}
else
{
//old author code
//popupMenu.style.left = document.body.scrollLeft + event.clientX;
popupMenu.style.left = 30;
}
if (bottomedge <popupMenu.offsetHeight)
{
popupMenu.style.top = document.body.scrollTop + event.clientY - popupMenu.offsetHeight;
}
else
{
popupMenu.style.top = document.body.scrollTop + event.clientY;
}
popupMenu.style.visibility = "visible ";
return false;
}
[解决办法]
http://topic.csdn.net/t/20040330/17/2904756.html
[解决办法]
有现成控件
[解决办法]
一般不会这么做的
都是当鼠标放上去,就出来一个类是菜单的框
[解决办法]
mark
[解决办法]
先在加一个contextMenu,在contextMenu中添加一些你需要的菜单选项