none
User Control Constructor and Child Controls RRS feed

  • Question

  • Hi to all,

    I have a simple usercontrol (UserControl1), that contains only 2 Bottons (button1 and button2). The property Visible of These 2 Bottons is true.

    I have then a form that contains only the usercontrol.

    *** CODE OF UserControl1 ***

    public partial class UserControl1 : UserControl
    {
     ArrayList CtrlColl = new ArrayList();

     public UserControl1()
     {
      InitializeComponent();

      CtrlColl.Add(button1);
      CtrlColl.Add(button2);

      Test1(); // << Line to comment

     }

     public void Test1()
     {
      Control ctrl;
      Int16 Counter = 0;


      ctrl = CtrlColl[0] as Control;
      ctrl.Visible = false;


      foreach (Control c in CtrlColl)
      {
       if (c.Visible)
       {
        Counter++;
       }
      }
      MessageBox.Show("Visible controls: " + Counter);
     }
    }

    *** /CODE OF UserControl1 ***

    When application runs, I see the message: "Visible controls: 1" .

    Ok, that works.  BUT if I comment the line in the constructor UserControl1()

    and  in the constructor Form1() I add  :  this.userControl11.Test1();

    the message is "Visible controls: 0" .

    That's not right, button2 is still visible!

    What I'm missing? Where is the Problem?

    Thanks.

    *** CODE OF Form1 ***

    public partial class Form1 : Form
    {
     public Form1()
     {
      InitializeComponent();

      this.userControl11.Test1();
     }
    }

    *** /CODE OF Form1 ***


    Tuesday, January 20, 2015 4:59 PM

Answers

  • Bill Chung provided you with the why and I provided you with a way through.

    The reason that it is not visible at the time of construction, the buttons belong to the user control, the user control belongs to the form and the form is not visible, so the user control is not visible and therefore the buttons are not visible.

    By moving the test call to the Form's Shown event, everything will work as expected.


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by Massimo XR1 Tuesday, January 20, 2015 9:51 PM
    Tuesday, January 20, 2015 7:39 PM

All replies

  • I think you mean 1 instead of 11.  You are rerfering to two different instances of the form.  "this" is the current instance of the form.  "this.Control1" is a different instance of the form that doesn't contain any buttons.

    jdweng

    Tuesday, January 20, 2015 5:56 PM
  • When you call Test1() Method in Constructor of the UserControl, it has not been added to Form.

    But When you call Test1() Methos in Constructor of Form, the usercontrol hase been added to a form, after added to form, the visibility of the usercontrol would be influenced by form.

    private void InitializeComponent()
            {
                this.SuspendLayout();
                this.userControl11 = new WindowsFormsApplication1.UserControl1();
    
                // this.userControl11.Test1(); <--- add to here , value will be 1
                // 
                // userControl11
                // 
                this.userControl11.Location = new System.Drawing.Point(50, 89);
                this.userControl11.Name = "userControl11";
                this.userControl11.Size = new System.Drawing.Size(262, 150);
                this.userControl11.TabIndex = 0;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(375, 357);
                this.Controls.Add(this.userControl11);
                // this.userControl11.Test1(); <--- add to here , value will be 0
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.ResumeLayout(false);
              
            }


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Tuesday, January 20, 2015 6:02 PM
  • If you move the test to the shown event, like so:

      private void Form1_Shown(object sender, EventArgs e)
      {
       this.userControl11.Test1();
      }
    

    you will get the results that you expect.


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Tuesday, January 20, 2015 6:08 PM
  • I mean  userControl11

    Here the code in Form1.Designer.cs

    *** partial code of class Form1 ***

    partial class Form1
     {

      ... ... ...

     private void InitializeComponent()
      {
       this.userControl11 = new TestShiftPosition.UserControl1();
       this.SuspendLayout();
       //
       // userControl11
       //
       this.userControl11.AutoSize = true;
       this.userControl11.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
       this.userControl11.BackColor = System.Drawing.Color.Moccasin;
       this.userControl11.Location = new System.Drawing.Point(12, 12);
       this.userControl11.Name = "userControl11";
       this.userControl11.Size = new System.Drawing.Size(81, 29);
       this.userControl11.TabIndex = 0;

      //
       // Form1
       //

       ... ... ...

    }

    Tuesday, January 20, 2015 7:04 PM
  • When I debug the application

    public Form1()
    {
     InitializeComponent();

     this.userControl11.Test1(); // << debug at this line
    }

    In the Immediate Window I get this:

    ?this.userControl11.button1.Visible

    false

    ?this.userControl11.button2.Visible

    false

    So after method Form1() is executed, something happens. Any idea?

    Tuesday, January 20, 2015 7:25 PM
  • Bill Chung provided you with the why and I provided you with a way through.

    The reason that it is not visible at the time of construction, the buttons belong to the user control, the user control belongs to the form and the form is not visible, so the user control is not visible and therefore the buttons are not visible.

    By moving the test call to the Form's Shown event, everything will work as expected.


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by Massimo XR1 Tuesday, January 20, 2015 9:51 PM
    Tuesday, January 20, 2015 7:39 PM
  • Thank you. It's all clear now.
    Tuesday, January 20, 2015 9:40 PM
  • No problem, have fun!!! It would be appreciated if you would mark any posts that you found helpful as helpful and any that answered your question as answers.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Tuesday, January 20, 2015 9:43 PM