我现在的需求是这样的1、WINFORM中根据menustrip已有的层次结构,在Treeview显示出对于的结构,这个需求已经做好了!2、在1的基础,当我将menustrip的几个选项的visiable属性设置为false时,Treeview上也不会显示,这个需求上有问题!!我加断点跟踪后发现,明明我大多数menustrip选项的visiable属性都是设置为Ture的,但程序运行起来就变为false了,紧急求教,谢谢!!
我的代码如下:private void ShowTree(TreeNode Pnode, ToolStripMenuItem itemcol)
        {
            for (int i = 0; i < itemcol.DropDownItems.Count; i++)
            {
               
                if (itemcol.DropDownItems[i].Visible ==true  )
                {
                    TreeNode Node = new TreeNode();
                    Node.Text = itemcol.DropDownItems[i].Text.Trim();
                    Node.Name = itemcol.DropDownItems[i].Name;
                    Pnode.Nodes.Add(Node);
                    ShowTree(Node, (ToolStripDropDownItem)itemcol.DropDownItems[i]);
                }
                else
                {
 
                }
            }
        }        private void ShowTree(TreeNode Pnode, ToolStripDropDownItem itemcol)
        {
            for (int i = 0; i < itemcol.DropDownItems.Count; i++)
            {
                
                if (itemcol.DropDownItems[i].Text.Trim() != "-" && itemcol.DropDownItems[i].Text.Trim() != "" )
                {
                    TreeNode Node = new TreeNode();
                        Node.Text = itemcol.DropDownItems[i].Text.Trim();
                        Pnode.Nodes.Add(Node);
                        ShowTree(Node, (ToolStripDropDownItem)itemcol.DropDownItems[i]);
                }
            }
        }        private void frmMain_Load(object sender, EventArgs e)
        {            ToolStripMenuItem itemcol=(ToolStripMenuItem) this.menuStripMain.Items[0];
            textBox2 .Text  =Convert .ToString ( itemcol.DropDownItems.Count);
            textBox1.Text =Convert .ToString ( itemcol.DropDownItems[0].Visible ) ;
            textBox3.Text = itemcol.DropDownItems[1].Text;
            try
            {
                for (int i = 0; i < this.menuStripMain.Items.Count; i++)
                {
                    TreeNode node = new TreeNode();
                   
                    if (this.menuStripMain.Items[i].Visible == true)
                    {
                        node.Text = this.menuStripMain.Items[i].Text.Trim();
                        node.Name = this.menuStripMain.Items[i].Name;
                        this.trvMain.Nodes.Add(node);
                        try
                        {
                            if (!this.menuStripMain.Items[i].GetType().ToString().ToUpper().Equals("ToolStripSeparator".ToUpper()))
                            {
                                ShowTree(node, (ToolStripMenuItem)this.menuStripMain.Items[i]);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    else
                    {                    }
                }
                this.trvMain .ExpandAll();
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载信息出错,错误信息如下:\n" + ex.Message);
            }
        }

解决方案 »

  1.   

    我就是想问的是为什么我没有设置默认的visiable属性,程序运行后会成false的
      

  2.   

    帮你发个颜色,没颜色太难看了
    private void ShowTree(TreeNode Pnode, ToolStripMenuItem itemcol)
      {
      for (int i = 0; i < itemcol.DropDownItems.Count; i++)
      {
        
      if (itemcol.DropDownItems[i].Visible ==true )
      {
      TreeNode Node = new TreeNode();
      Node.Text = itemcol.DropDownItems[i].Text.Trim();
      Node.Name = itemcol.DropDownItems[i].Name;
      Pnode.Nodes.Add(Node);
      ShowTree(Node, (ToolStripDropDownItem)itemcol.DropDownItems[i]);
      }
      else
      {
      
      }
      }
      }  private void ShowTree(TreeNode Pnode, ToolStripDropDownItem itemcol)
      {
      for (int i = 0; i < itemcol.DropDownItems.Count; i++)
      {
        
      if (itemcol.DropDownItems[i].Text.Trim() != "-" && itemcol.DropDownItems[i].Text.Trim() != "" )
      {
      TreeNode Node = new TreeNode();
      Node.Text = itemcol.DropDownItems[i].Text.Trim();
      Pnode.Nodes.Add(Node);
      ShowTree(Node, (ToolStripDropDownItem)itemcol.DropDownItems[i]);
      }
      }
      }  private void frmMain_Load(object sender, EventArgs e)
      {  ToolStripMenuItem itemcol=(ToolStripMenuItem) this.menuStripMain.Items[0];
      textBox2 .Text =Convert .ToString ( itemcol.DropDownItems.Count);
      textBox1.Text =Convert .ToString ( itemcol.DropDownItems[0].Visible ) ;
      textBox3.Text = itemcol.DropDownItems[1].Text;
      try
      {
      for (int i = 0; i < this.menuStripMain.Items.Count; i++)
      {
      TreeNode node = new TreeNode();
        
      if (this.menuStripMain.Items[i].Visible == true)
      {
      node.Text = this.menuStripMain.Items[i].Text.Trim();
      node.Name = this.menuStripMain.Items[i].Name;
      this.trvMain.Nodes.Add(node);
      try
      {
      if (!this.menuStripMain.Items[i].GetType().ToString().ToUpper().Equals("ToolStripSeparator".ToUpper()))
      {
      ShowTree(node, (ToolStripMenuItem)this.menuStripMain.Items[i]);
      }
      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }
      }
      else
      {  }
      }
      this.trvMain .ExpandAll();
      }
      catch (Exception ex)
      {
      MessageBox.Show("加载信息出错,错误信息如下:\n" + ex.Message);
      }
      }问题的保姆,帖子的管家——《Csdn收音机》!