public void AddMyControls1(int pos_width, int pos_height, string text, string name)
        {
            Label UserControl = new Label();
            UserControl.Name = "lbl" + name;
            UserControl.Text = text;
            UserControl.ForeColor = SystemColors.ControlLightLight;
            UserControl.Location = new Point(6, 30 + pos_height);
            UserControl.Size = new Size(53, 12);
            //UserControl.TopLevelControl = this.groupBox5;
            //this.groupBox5.Controls.Add(UserControl);
            this.groupBox5.Controls.Add(UserControl);
            LabArray[TxtNum - 2] = UserControl;
    
        }我 用这段代码在groupBox5添加LABEL控件没有效果,
但把这句
this.groupBox5.Controls.Add(this.label1);
写到主函数里却是有效果的我问下是那里出了问题啊

解决方案 »

  1.   

    你添加到Controls的UserControl应该设置Visible属性为True才行:
    UserControl.Visible=true;
      

  2.   

    public void AddMyControls1(int pos_width, int pos_height, string text, string name)
            {
                Label UserControl = new Label();
                UserControl.Name = "lbl" + name;
                UserControl.Text = text;
                UserControl.ForeColor = SystemColors.ControlLightLight;
                UserControl.Location = new Point(6, 30 + pos_height);
                UserControl.Size = new Size(53, 12);
                //UserControl.TopLevelControl = this.groupBox5;
                //this.groupBox5.Controls.Add(UserControl);
                UserControl.Visible = true;            this.groupBox5.Controls.Add(UserControl);
                //MessageBox.Show("aa");
                LabArray[TxtNum - 2] = UserControl;
        
            } case 2:
                            AddMyControls1(0, 322, "内容一:", "2");这样是没效果的
    放到这个函数里不行 
    放到函数外面就可以了      case 2:
     
                            Label aa = new Label();
                            aa.Text = "sssssssssssss";
                            this.groupBox5.Controls.Add(aa)这样是可以的
    通过
     this.groupBox5.Controls.Add(UserControl);
                MessageBox.Show("aa");
    测试函数已经执行到那步了
    就是在窗体上看不到生成的控件
      

  3.   

    是不是你生成控件的Location已经超出了你这个groupBox5的范围了