問題描述
我的用戶控件繼承了 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?
注意:我嘗試覆蓋 Contorl
的 Visible
屬性,但它不可覆蓋.
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;
}
這篇關于如何識別控件的可見性是否被用戶更改?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!