I have 2 forms. in form 1, I have a treeview. in one method, I get list of nodes that checked.
private void CreateListOfCheckedNodes()
{
TreeNodeCollection nodes = TreeData.Nodes;
Int32 EffectedNodes = GetCheckedNodesCount(nodes);
if (EffectedNodes > 0)
{
;//my code
}
}
public int GetCheckedNodesCount(TreeNodeCollection nodes)
{
int CheckedNodesCount = 0;
for (int i = 0; i < nodes.Count; i++)
{
TreeNode node = nodes[i];
if (node.Checked)
{
TreeFieldSet Item = new TreeFieldSet { TreeId = Int32.Parse(node.Tag.ToString()), Title = node.Text };
MyProject.Tree.TreeCheckedNodes.TreeList.Add(Item);
CheckedNodesCount++;
}
if (node.Nodes.Count > 0)
CheckedNodesCount += GetCheckedNodesCount(node.Nodes);
}
return CheckedNodesCount;
}
How can I calling method CreateListOfCheckedNodes in Form 2 without new of Form1 ?
With this calling :
Tree.TreeSubjects Ts = new Tree.TreeSubjects();
Ts.CreateListOfCheckedNodes();//if CreateListOfCheckedNodes public
I get error !
Please Help !
Move the code into a new class that can be accessed by both forms.
You can make the method static if it doesn’t use any member variables (a quick glance at the code seems to suggest that this is the case).
You could also consider making it an extension method of the TreeNodeCollection class, but this should be done with caution as it may surprise your colleagues.