如何在树视图中显示项目

如何在树视图中显示项目

本文介绍了如何在树视图中显示项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我想在项目中显示我的树视图表格并点击它们打开表格...

请帮助!

我用过这段代码:



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 :

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++;





在treeView中显示菜单;



现在我想显示表格,例如:



表格1 =节点1

表格2 =节点2



如果表格,以下组的形式:



表格1 =节点1

-----表格3 =节点3

-----表格4 =节点4

表格2 =节点2



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

推荐答案

Form1
    Form5
    Form6
Form2
    Form7
    Form8
        Form11
        Form12
            Form13
    Form9
Form3
Form4
    Form10

代码示例:在MainForm上假设一个主窗体MainForm,一个ToolMenuStrip,menuStrip1和一个Button'btnBuildTree。

Code example: assume a main form, MainForm, on MainForm a ToolMenuStrip, menuStrip1, and a Button 'btnBuildTree.

// 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
    }
}

评论:



1.虽然这样可行(尝试一下):更好的策略是使用Key Type为'的字典' TreeNode和Key的Value Type是'形成对你创建的各种表单的每个实例的引用。



2.好奇的问题:为什么你需要两者您的应用程序表单结构的TreeView和基于菜单的表示形式?

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 ?


这篇关于如何在树视图中显示项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:28