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找了半天没找到。
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比较的应该是集合内是否存在同一对象,貌似是比较地址的
Nodes.Contains()方法是查找该节点的子节点中是否存在指定的节点.它不能寻找子节点的子节点