PDA

View Full Version : مشكل در كار با Treeview



hadisadaghatmehr
دوشنبه 27 دی 1389, 23:26 عصر
با سلام خدمت دوستان

من تو برنامه ام يه treeview ايجاد كه چند تا root داره و هر كدام ار اين root ها چند گره child هم دارن من مي خوام كدي بنويسم كه وقتي گره root انتخاب شد(check box ش) تمام گره هاي child هم انتخاب بشن حالا نمي دونم اين كد رو تو كدوم رويداد بنويسم و چه كدي بنويسم
ممنون ميشم اگه راهنمايي بفرماييد

Reza_Yarahmadi
سه شنبه 28 دی 1389, 07:54 صبح
ميتونيد بصورت زير عمل كنيد

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
TreeNode tn = e.Node;
if (tn.Nodes.Count > 0)
SetAllChildValue(tn.Nodes, tn.Checked);
}
void SetAllChildValue(TreeNodeCollection childs, bool value)
{
foreach (TreeNode tn in childs)
{
tn.Checked = value;
if (tn.Nodes.Count > 0)
SetAllChildValue(tn.Nodes, value);
}
}

shuriken
سه شنبه 28 دی 1389, 08:23 صبح
سلام
کد زیر کل کارهایی رو که شما نیاز به انجامش دارین بهتون نشون میده.
فقط کافیه رخداد معرفی شدرو برای node مورد نظر handle کنین.
اگرم یمقدار بالا پایینش کنین چیزهای خیلی بیشتری پیدا میکنین.


// Updates all child tree nodes recursively.
private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
foreach(TreeNode node in treeNode.Nodes)
{
node.Checked = nodeChecked;
if(node.Nodes.Count > 0)
{
// If the current node has child nodes, call the CheckAllChildsNodes method recursively.
this.CheckAllChildNodes(node, nodeChecked);
}
}
}

// NOTE This code can be added to the BeforeCheck event handler instead of the AfterCheck event.
// After a tree node's Checked property is changed, all its child nodes are updated to the same value.
private void node_AfterCheck(object sender, TreeViewEventArgs e)
{
// The code only executes if the user caused the checked state to change.
if(e.Action != TreeViewAction.Unknown)
{
if(e.Node.Nodes.Count > 0)
{
/* Calls the CheckAllChildNodes method, passing in the current
Checked value of the TreeNode whose checked state changed. */
this.CheckAllChildNodes(e.Node, e.Node.Checked);
}
}
}

موفق باشین