首页 新闻 赞助 找找看

Treeview的nodes.contains困惑

0
悬赏园豆:50 [已解决问题] 解决于 2011-01-12 13:34

 

if (private_Node != null)
{
private_Node.ChildNodes.Clear();
}
private_Node
= new TreeNode();
private_Node.Text
= TextBox3.Text;
private_Node.Value
= TextBox3.Text;

if (!TreeView3.Nodes.Contains(private_Node))
{

TreeView3.Nodes.Add(private_Node);
}
else
{
Page.RegisterStartupScript(
"Startup", "<script>alert(\"节点名称存在! \")</script>");
}

 

想要检测当前目录下是否存在相同的节点名称,除了遍历还有其他方法不?

 

ListItem temp = new ListItem();
temp.Text
= TextBox4.Text;
if (!ListBox1.Items.Contains(temp))
{

ListBox1.Items.Add(temp);
}

 

 

上面用listbox的代码可以,但treeview上面的代码是无效的

 

请问有否知道Nodes.Contains()具体工作方式的不?

 

msdn找了半天没找到。

massinger的主页 massinger | 小虾三级 | 园豆:706
提问于:2011-01-10 15:42
< >
分享
最佳答案
0
public bool Contains(TreeNode node)
{
return (this.IndexOf(node) != -1);
}

public int IndexOf(TreeNode node)
{
for (int i = 0; i < this.Count; i++)
{
if (this[i] == node)
{
return i;
}
}
return -1;
}


Reflector看的代码

 

Contains比较的应该是集合内是否存在同一对象,貌似是比较地址的

收获园豆:50
六芒星 | 小虾三级 |园豆:627 | 2011-01-11 15:28
兄弟正解啊,listbox的那个功能不错,省去了比较相同Text的麻烦,考虑给treeview添加个比较同一级目录下相同文本比较功能。 话说,居然忘了Reflector,罪过啊。
massinger | 园豆:706 (小虾三级) | 2011-01-12 13:33
其他回答(1)
0

Nodes.Contains()方法是查找该节点的子节点中是否存在指定的节点.它不能寻找子节点的子节点

guoxuefeng | 园豆:240 (菜鸟二级) | 2011-01-11 10:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册