public class TreeNodeAdvExt : TreeNodeAdv
{
public TreeNodeAdvExt()
{
}
/// <summary>
/// Holds the Node name
/// </summary>
private string m_Name = string.Empty;
/// <summary>
/// Gets/Sets the Node name
/// </summary>
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
}
private void TreeViewAdv1_MouseUp(object sender, MouseEventArgs e)
{
if (this.treeViewAdv1.GetNodeAtPoint(e.Location) != null && this.treeViewAdv1.GetNodeAtPoint(e.Location) is TreeNodeAdvExt)
MessageBox.Show("Node Name is: " + (this.treeViewAdv1.GetNodeAtPoint(e.Location) as TreeNodeAdvExt).Name.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
treeNodeAdvExt1.Name = "Node 0";
treeNodeAdvExt2.Name = "Node 1";
treeNodeA3vExt3.Name = "Node 2";
treeNodeAdvExt4.Name = "Node 3";
treeNodeAdvExt5.Name = "Node 4";
treeNodeAdvExt6.Name = "Node 5";
treeNodeAdvExt7.Name = "Node 6";
}
|
Hi, thank you for your fast answer!
It is working good but I have another problem:
TreeNodeAdvExt cnode = new TreeNodeAdvExt();
cnode.Text = "xxxxx";
cnode.Name = "cnode1";
//it is ok
treeNodeAdvExt1.Nodes[0].Nodes.Add(cnode);
//but it is not working - obviously. Regular TreeView supports this.
treeNodeAdvExt1.Nodes["Node 4"].Nodes.Add(cnode);
I can search by name but I have large trees (100 000+ nodes, including a multicolumn). I think search won't be too efficient.
TreeNodeAdv node = GetNodeByName(this.treeViewAdv1.Nodes, this.nodeNameCmb.SelectedItem.ToString());
/// <summary>
/// Gets the TreeNodeAdv name
/// </summary>
/// <param name="nodes">Node Collection</param>
/// <param name="Name">Node Name</param>
/// <returns>TreeNodeAdv</returns>
public static TreeNodeAdv GetNodeByName(TreeNodeAdvCollection nodes, string Name)
{
TreeNodeAdv matchNode = null;
bool isMatchFound = false;
foreach (object node in nodes)
{
if (node is TreeNodeAdvExt && (node as TreeNodeAdvExt).Name == Name)
{
isMatchFound = true;
matchNode = node as TreeNodeAdv;
return matchNode;
}
if (!isMatchFound)
{
matchNode = GetNodeByName((node as TreeNodeAdvExt).Nodes, Name);
if (matchNode != null)
{
return matchNode;
}
}
}
return null;
}
|