Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I want to show my treeview forms in the project and open the form by clicking on them ...
Please help!
I used this code :

C#
Main mn = new Main(); // main form
int c = mn.menuStrip1.Items.Count;
foreach (ToolStripMenuItem menu in mn.menuStrip1.Items)
{
    menu.Visible = true;
    treeView1.Nodes.Add("x", menu.Text);
}
foreach (ToolStripMenuItem menu in mn.menuStrip1.Items)
{
    menu.Visible = true;
    int j = 0;
    for (int i = 0; i < menu.DropDown.Items.Count; i++)
    {
        treeView1.Nodes[j].Nodes.Add("X", menu.DropDown.Items[i].Text);
    }
    j++;


To display menus in treeView ;

And now I want to display forms, for example:

Form 1 = Node 1
Form 2 = Node 2

If the form, the form of the following groups:

Form 1 = Node 1
----- Form 3 = Node 3
----- Form 4 = Node 4
Form 2 = Node 2
Posted
Updated 9-Dec-14 23:30pm
v2
Comments
Dipak Y Salve 9-Dec-14 22:34pm    
Can you elaborate more please?
BillWoodruff 9-Dec-14 23:23pm    
So, you have a WinForms Application project ?

It has a TreeView ? More than one TreeView ?

And when you click on a Node in the TreeView: it opens a Form ? Opens a Form where ?

easy to do, I think, once we understand what you are trying to achieve.
OriginalGriff 10-Dec-14 5:18am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
And there are so vary many different things you could be trying to do...
Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 10-Dec-14 5:32am    
Don't post extra information as Solutions, use the Improve question link and add it to your original post.
ghasem110deh 10-Dec-14 6:58am    
Excuse me
I can not speak English well
I have a project with 23 Form (19 Forms primary and 4 secondary forms)
Now i have display all the forms in treeview then click on the name of a node treeView , the open form

1 solution

I'm going to assume that you need recursion, that your menu structure could have more than one level of nesting, like this:
C#
Form1
    Form5
    Form6
Form2
    Form7
    Form8
        Form11
        Form12
            Form13
    Form9
Form3
Form4
    Form10
Code example: assume a main form, MainForm, on MainForm a ToolMenuStrip, menuStrip1, and a Button 'btnBuildTree.
C#
// map TreeNodes to MenuItems ...
private Dictionary<TreeNode, ToolStripMenuItem> dctTSMenuToTNode;

private void MainForm_Load(object sender, EventArgs e)
{
    dctTSMenuToTNode = new Dictionary<treenode,>();
}

private void btnBuildTree_Click(object sender, EventArgs e)
{
    dctTSMenuToTNode.Clear();

    buildTree(menuStrip1.Items, treeView1.Nodes);
}

private void buildTree(ToolStripItemCollection menuItems, TreeNodeCollection tNodes)
{
    foreach (ToolStripMenuItem menuItem in menuItems)
    {
        menuItem.Visible = true;

        var newNode = new TreeNode(menuItem.Text);

        tNodes.Add(newNode);

        newNode.Name = (newNode.Level == 0)
            ? "x"
            : "X";

        dctTSMenuToTNode.Add(newNode, menuItem);

        // recurse if necessary
        if (menuItem.HasDropDownItems)
        {
            buildTree(menuItem.DropDownItems, newNode.Nodes);
        }
    }
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    TreeNode currentNode = e.Node;

    // for testing only
    MessageBox.Show(string.Format("Node: {0} Name: {1}", dctTSMenuToTNode[currentNode].Text, currentNode.Name));

    // your code to select/show Forms goes here
    // pull the Form Name out of the Dictionary
    switch (dctTSMenuToTNode[currentNode].Text)
    {
        case "Form1":
            // do something with Form1
            break;
        // write other case statements here
    }
}
Comments:

1. While this will work (try it): a better strategy would be to use a Dictionary where the Key Type is 'TreeNode, and the Value Type of the Key is 'Form a reference to each instance of the various Forms you create.

2. Curious question: why do you need both a TreeView and a Menu based representation of your App's Forms structure ?
 
Share this answer
 
v2
Comments
ghasem110deh 10-Dec-14 8:19am    
thanks ...
error on character ">"
Error 1 Type expected
BillWoodruff 10-Dec-14 21:07pm    
fixed error evidently caused by CP's editor handling of pasted in code.

Note that in the WinForms MS TreeView, the 'Key property of a TreeNode is the same as the 'Name property.

:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900