如何确定一个控制的知名度? [英] How do I determine visibility of a control?

查看:169
本文介绍了如何确定一个控制的知名度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.aspx"><$c$c>TabControl包含几个选项卡。每个标签都有一个<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol.aspx"><$c$c>UserControl在上面。我想检查的控制 X 用户控件 A 用户控件 B 。我想,做 x.Visible 用户控件 B 会不够好。事实证明,这是展示中,即使我明确地将它设置为调试器,这是从来没有改变。然后我读MSDN上的<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.visible.aspx">Control.Visible即:

I have a TabControl that contains several tabs. Each tab has one UserControl on it. I would like to check the visibility of a control x on UserControl A from UserControl B. I figured that doing x.Visible from UserControl B would be good enough. As it turns out, it was displaying false in the debugger even though I set it explicitly to true and it was never changed. Then I read on MSDN for Control.Visible that:

即使可见被设置为true,控制可能是不可见的用户,如果它是模糊其他控件的后面。

Even if Visible is set to true, the control might not be visible to the user if it is obscured behind other controls.

所以,出乎我的意料,这是行不通的。现在我想知道我怎么能告诉如果控制 X 可见从不同的用户控件。我想避免使用布尔如果可能的话。有没有人遇到这个,发现一个解决办法?

So much to my surprise, that will not work. Now I'm wondering how I can tell if the control x is visible from a different UserControl. I would like to avoid using a boolean if possible. Has anyone run into this and found a solution?

请注意:它也出现了 Control.IsAccessible 为假在这种情况下

Note: It also appears that Control.IsAccessible is false in this situation.

推荐答案

不幸的是,控制不提供任何市民,可以让你检查一下。

Unfortunately the control doesn't provide anything public that will allow you to check this.

一种可能性是设置一些在控制标签属性。标签的用途是将用户数据与控制关联。因此,它可以是任何东西不仅仅是一个布尔值。

One possibility would be to set something in the controls 'Tag' property. The tag’s purpose is to associate user data with the control. So it can be anything not just a boolean.

这里是标记属性DOC

如果你真的想要的强制方法,您可以使用反射,基本要求GETSTATE(2):

If you really want the brute force way, you can use Reflection, basically calling GetState(2):

public static bool WouldBeVisible(Control ctl) 
{
      // Returns true if the control would be visible if container is visible
      MethodInfo mi = ctl.GetType().GetMethod("GetState", BindingFlags.Instance | BindingFlags.NonPublic);
      if (mi == null) return ctl.Visible;
      return (bool)(mi.Invoke(ctl, new object[] { 2 }));
}

这篇关于如何确定一个控制的知名度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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