如何识别控件的可见性是否被用户更改? [英] How to identify whether visibility of the control is changed by user or not?

查看:25
本文介绍了如何识别控件的可见性是否被用户更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户控件继承了 System.Windows.Forms.Control 类.以下链接描述了控件的可见"属性控制.可见

My usercontrol inherits System.Windows.Forms.Control class. The following link describes the "Visible" property of control Control.Visible

根据上面的链接,如果控件存在于非活动选项卡中,那么即使我们没有以编程方式设置 Control.Visible 也会返回 false

As per the above link, if control is present in inactive tab, then Control.Visible will return false even though we did not set it programmatically

问题:如何确定可见性是否被用户或其他控件禁用?

Question: How do I identify whether visibility was disabled by user or other controls?

注意:我尝试覆盖 ContorlVisible 属性,但它不可覆盖.

Note: I tried overriding the Visible property of Contorl but it's not overridable.

说明

如果我的控件存在于未选择的选项卡中,则 Control.Visible 返回 false.如果用户想在 Bitmap 或其他东西中绘制控件(导出),我还需要确定子控件的可见性.由于我的控件不可见,因此没有可靠的方法来确定子控件的可见性

If my control is present in unselected tab, then Control.Visible returns false. If the user wants to draw the control (export) in a Bitmap or something else, I need to determine the visibility of child controls too. Since my control is not visible, there is no reliable way available to determine the visibility of child controls

推荐答案

windows 窗体中的所有控件在内部保持其状态.可见性也是他们保持状态的事情之一.因为它有助于确定控件的可见性发生更改的原因.

All controls in windows forms internally maintain their state. Visibility is also one of the things they maintain in state. Because it helps to identify why visibility of the control was changed.

Control.Visible 如果您的上方有一个控件,则将返回 false控件或控件的父级被隐藏.但可见的价值只有当用户将其设置为 false 时,state 中的属性才会为 false.

Control.Visible will return false if there is a control above your control or parent of your control is hidden. But value of Visible property in state will be false only if user set it to false.

代码:

        //Method to ensure the visibility of a control
        public bool DetermineVisibility(Control control)
        {
            //Avoid reflection if control is visible
            if (control.Visible)
                return true;

            //Find non-public GetState method of control using reflection
            System.Reflection.MethodInfo GetStateMethod = control.GetType().GetMethod("GetState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

            //return control's visibility if GetState method not found
            if (GetStateMethod != null)     
                //return visibility from the state maintained for control
                return (bool)(GetStateMethod.Invoke(control, new object[] { 2 }));
            return false;
        }

这篇关于如何识别控件的可见性是否被用户更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆