xpblueScreenOfDeath
Programmer
- Sep 1, 2004
- 87
I created a lazy loading treeview. The tree only has 2 levels. I will allows load the first level. The second is loaded only if the user expands it. To make the plus appear for nodes that contains chidrens, I add one node with a empty string under each first level nodes. The only code that deals with expanding and collapsing a node is in the MouseDown event. Sometimes the node won't collapse but it would clear the child nodes and add one blank text node. If someone can tell me what I am doing wrong it would be great. Thanks.
Code:
private void phoneTree_MouseDown(object sender, MouseEventArgs e)
{
System.Windows.Forms.TreeView tv = (System.Windows.Forms.TreeView)sender;
System.Windows.Forms.TreeNode n = tv.GetNodeAt(e.X, e.Y);
System.Windows.Forms.TreeNode childNode;
System.Data.DataTable dt;
System.Data.DataRow dRow;
string[] values;
int i;
if (n != null && n.Level == 0)
{
n.Nodes.Clear();
values = n.Tag.ToString().Split('_');
if (int.Parse(values[1]) > 0)//node has sublevels
{
if (!n.IsExpanded)
{
dt = fill in data table;
for (i = 0; i < dt.Rows.Count; i++)
{
dRow = dt.Rows[i];
childNode = new System.Windows.Forms.TreeNode();
childNode.Tag = string.Format("{0}_0", dRow["ID"]);//ID_hasChildren
childNode.Text = dRow["Desc"].ToString();
n.Nodes.Add(childNode);
}
}
else
{
n.Nodes.Clear();
childNode = new System.Windows.Forms.TreeNode();
n.Nodes.Add(childNode);
}
}
}
}