且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# UserControl 可见属性不变

更新时间:2022-12-03 12:04:00

我没有破坏 C#!:)

I didn't break C#! :)

原来罪魁祸首是 Form.Visible 属性.在 Form.Visible 设置为 true 之前,表单上的所有控件都将不可见(Visible = false).

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.

但是,您仍然可以设置可见属性 - 它们只是在 Form.Visible 属性设置为 true 之前不会生效.

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

换句话说,当我调用 ucFollow.Visible = true 时,我的程序确实在注册它 - 然而,在代码中,ucFollow 的父 Form.Visible 仍然是 false.因此,调试器和我的打印语句都识别出,嘿,这个控件的父窗体仍然不可见,所以这个控件不可见.句号."

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.