How to get the “real” value of the Visible property?

前端 未结 4 949
执笔经年
执笔经年 2021-01-11 11:40

If you set the Visible property of a Windows Forms control to true, that property still returns false if any of the control\'s parent windows are hidden. Is there a way to g

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-11 12:14

    Well, the regular implementation does check up the control stack, to ensure that all parents are visible. The only way I know to dodge this is to cheat with reflection, and ask for GetState(2), but that is brittle:

        // dodgy; not recommended
        Panel query;
        Form form = new Form
        {
            Controls = {
                new Panel {
                    Visible = false,
                    Controls = {
                        (query = new Panel {Visible = true})
                    }
                }
            }
        };
        form.Show();
    
        // this is the dodgy bit...
        bool visible = (bool)typeof(Control)
            .GetMethod("GetState", BindingFlags.Instance | BindingFlags.NonPublic)
            .Invoke(query, new object[] { 2 });
    

提交回复
热议问题