2013-02-26 30 views
0

我在我的winform中会显示多个组框,它们将基于树视图选择显示,我只想在所有组中使用一个按钮,我必须调用唯一方法基于树视图选择,如何做到这一点?在win窗体中的多个组框中的一个按钮

+0

您是否在运行时构建组合框? – 2013-02-26 19:43:02

+0

不,它在设计时 – Dev 2013-02-27 09:20:13

回答

1

要做些什么,这取决于树形视图你选择,你可以做到这一点你的TreeView控件的AfterSelect事件节点,(supossing你,1个TreeView控件,4个GroupBoxes和一个按钮和一个名为Button1的):

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) 
     { 
      //Get current selected node 
      TreeNode treenode = treeView1.SelectedNode; 

      //Position the button so it will be visible, change according needs 
      button1.Location = new Point(20, 20); 

      //I'm making the selection using "Text" property 
      switch (treenode.Text) 
      { 
       case "1": 
        changeVisible(groupBox1); //Hide all GroupBoxes excep groupBox1 
        groupBox1.Controls.Add(button1);//Add the button1 to GroupBox1 Controls property 

        //You can execute a specific ethod for this case here. 
        //DoSomethingForTreeNode1(); 

        break; 
       case "2": 
        changeVisible(groupBox2); 
        groupBox2.Controls.Add(button1); 
        break; 
       case "3": 
        changeVisible(groupBox3); 
        groupBox3.Controls.Add(button1); 
        break; 
       case "4": 
        changeVisible(groupBox4); 
        groupBox4.Controls.Add(button1); 
        break; 
      } 
     } 


     //The only purpouse of this method is to hide all but the desired GroupBox control 
     private void changeVisible(GroupBox groupBox) 
     { 
      //Loop across all Controls in the current Form 
      foreach (Control c in this.Controls) 
      { 
       if(c.GetType() == typeof(GroupBox)) 
       { 
        if(c.Equals(groupBox)) 
        { 
         c.Visible = true; 

        } 
        else 
        { 
         c.Visible = false; 
        } 

       } 

      } 
     } 

希望它有帮助,

相关问题