C# UserControl Visible Property Not Changing

后端 未结 3 1337
臣服心动
臣服心动 2020-12-17 10:57
Debug.WriteLine(ucFollow.Visible);
ucFollow.Visible = true;
Debug.WriteLine(ucFollow.Visible);

ucFollow is a custom UserControl, nothing fancy. The

3条回答
  •  再見小時候
    2020-12-17 11:15

    I didn't break C#! :)

    Turns out the culprit was the Form.Visible property. Before Form.Visible is set to true, any and all controls on the form will be invisible (Visible = false) no matter what.

    However, you can still set Visible properties - they just won't take effect until the Form.Visible property is set to true.

    In other words, when I called ucFollow.Visible = true, my program was indeed registering it - however, at that point in the code, ucFollow's parent Form.Visible was still false. Therefore, both the Debugger and my print statements recognized, "Hey, this control's parent form is still not visible, so this control is not visible. Period."

    As soon as the form was made visible, all the changes took effect and everything worked great.

    Moral of the story: Don't rely on the Visibility properties of your controls unless the form containing them is already visible and running.

提交回复
热议问题