Today I found this strange behavior that any Winform's any control's visible property is not initiated anywhere until the parent Form's "show()" function got called. Until that the visible property is initiated to "false". I checked the designer.cs, here also when the visible property in the "design window" got changed into false, then it is appearing in the designer.cs. But when I changed the visible property of any control into true, then the visible property is not appearing in the designer.cs. But when the parent's "show()" function got called at that time only, the control's visible property got changed into true or the latest value assigned to it in the constructor. I tried to assign the visible property from outside before calling the shown, here also assigning true to visible is not at all working. It seems visible property is unreliable. Sample code is shown below
Form2 tf = new Form2();
bool groupBox_temp1 = ((GroupBox)(tf.Controls.Find("groupBox1", true).FirstOrDefault())).Visible;
bool button2_temp1 = ((Button)(tf.Controls.Find("button2", true).FirstOrDefault())).Visible;
Button button2 = (Button)(tf.Controls.Find("button2", true).FirstOrDefault());
button2.Visible = true;
bool button2_V = ((Button)(tf.Controls.Find("button2", true).FirstOrDefault())).Visible;
tf.Show();
bool groupBox_temp2 = ((GroupBox)(tf.Controls.Find("groupBox1", true).FirstOrDefault())).Visible;
bool button2_temp2 = ((Button)(tf.Controls.Find("button2", true).FirstOrDefault())).Visible;
Can anybody tell me why it is happening? Is it advisable to use visible property in "if" conditions. If it is not reliable , then what is the best approach?
Note: I am using visual c# express 2008,.net 3.5