for some reason this code throws me an error
private void button1_Click(object sender, EventArgs e)
{
treeView1.BeginUpdate();
TreeNode labelnode = treeView1.Nodes[0].Nodes.Add("Label" + labelnum.ToString());
treeView1.EndUpdate();
//create id
string id = "1";
Thread thread = new Thread(new ThreadStart(Worker(labelnode, id))); // it errors out here
thread.Start();
}
private void Worker(TreeNode labelnode, string id)
{
while (true)
{
Thread.Sleep(50);
if (labelnode.IsSelected == true)
{
showProperties("label", id);
}
}
}
what i was trying to do here is show properties when labelnode was selected. would be glad and thankful if someone helped me
>Solution :
It seems that what the code is trying to do is check whether a node is selected. The proper way to do this is to use the TreeView.AfterSelect event. Using a thread like this will peg a CPU core at 100% doing nothing.
private void TreeView1_AfterSelect(System.Object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
if (e.Node==treeView1.Nodes[0]) //Or whatever condition you want
{
showProperties("label", e.Node.Tag);
}
}
In Windows Forms almost all controls have a Tag property that can be used to store anything the application wants. You can store the ID value in the new node’s tag :
TreeNode labelnode = treeView1.Nodes[0].Nodes.Add("Label" + labelnum.ToString());
labelNode.Tag="1";