0

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

prabhakaran
  • 5,126
  • 17
  • 71
  • 107
  • 1
    First of all what you are doing is terrible, consider to either expose properties of `Form2` (if it's a kind of custom control) or rather add code to achieve wanted inside(e.g. toggling button visibility could be done via property `ShowOkButton`). Secondly - please refer to [mcve], you didn't show enough code to a) present us a problem (you are saying it doesn't work, but without proof it's may or may **not** be true), b) let someone check it for you. – Sinatr Oct 30 '20 at 08:34
  • @Sinatr I found the root cause of my problem, that is the property visible is not working like other properties. This is common behavior for any form. You just create a form with button in it. Change the visible property as true in design window. Now create the form using constructor and check the button's visible property. Then call the form's "show" method. Then check the button's property. You will get the idea. I just want to know why it is happening? – prabhakaran Oct 30 '20 at 08:36
  • I guess you are reading `Visibility` property after setting it to `true` and since it return `false` you and thinking it will be invisible? No, it will be correctly shown (unless you made some other mistake). You are getting `false`, because [getter](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Control.cs,4373) is returning `false` for hidden parent, until you show the parent, check [this comment](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Control.cs,6651). – Sinatr Oct 30 '20 at 09:06
  • [Here](https://stackoverflow.com/q/391888/1997232) and [here](https://stackoverflow.com/q/27596529/1997232) are relevant questions of how to check visibility of control belonging to invisible parent. – Sinatr Oct 30 '20 at 09:10
  • @Sinatr Thank you for the links. So as I thought visible property is not reliable. The links just made this point clear – prabhakaran Oct 30 '20 at 09:37

0 Answers0