如何确定控件是否具有Text属性 [英] How to determine if Control has Text property

查看:46
本文介绍了如何确定控件是否具有Text属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我遍历窗体上的许多不同控件时,而不是尝试访问Text属性:

When I iterate over a bunch of different controls on a Form, instead of trying to access the Text property:

String text = String.Empty;
foreach(Control control in this.Controls)
{
   try
   {
      text = control.Text;
   }
   catch(Exception exception)
   {
      // This control probably doesn't have the Text property.
      Debug.WriteLine(exception.Message);
   }
}

有没有一种方法可以确定给定控件是否具有Text属性?像这样:

Is there a way to just determine whether or not a given control has a Text property? Something like this:

String text = String.Empty;
foreach(Control control in this.Controls)
{
   if(control has Text property)
   {
      text = control.Text;
   }
}

我绝对鄙视尝试/捕获"功能块(除非当然没有更好的选择).

I absolutely despise the Try/Catch blocks (unless there is no better alternative, of-course).

推荐答案

所有 Control 对象都有一个

All Control objects have a Text property, so there is no point in using reflection to determine that. It will always return true.

您的问题实际上是某些控件从其 Text 属性抛出异常,因为它们不支持该异常.

Your problem actually is that some controls throw an exception from their Text property because they don't support it.

如果您还希望能够使用事先不知道的自定义控件,则应坚持使用当前的解决方案并捕获异常.但是,您应该捕获引发的特定异常,例如 NotSupportedException .

If you also want to be able to use custom controls that you don't know in advance, you should stick to your current solution and catch the exceptions. However, you should catch the specific exception thrown, for example NotSupportedException.

如果仅遇到预先知道的控件,则可以选择已知具有有效的 Text 属性的控件.例如:

If you only ever encounter controls that you know in advance, you can select the controls that you know have a working Text property. For example:

public static bool HasWorkingTextProperty(Control control)
{
    return control is Label
        || control is TextBox
        || control is ComboBox;
}

var controlsWithText = from c in this.Controls
                       where HasWorkingTextProperty(c)
                       select c;

foreach(var control in controlsWithText)
{
    string text = control.Text;
    // Do something with it.
}

如果您实现自己的自定义控件,而该控件可能具有或不具有 Text 属性,则可以从表明这一点的基类派生它们:

And if you implement your own custom controls that may or may not have a Text property, then you can derive them from a base class that indicates this:

public abstract class CustomControlBase : Control
{
    public virtual bool HasText
    {
        get { return false; }
    }
}

public class MyCustomControl : CustomControlBase
{
    public override bool HasText
    {
        get { return true; }
    }

    public override string Text
    {
        get { /* Do something. */ }
        set { /* Do something. */ }
    }
}

public static bool HasWorkingTextProperty(Control control)
{
    return (control is CustomControlBase && ((CustomControlBase)control).HasText)
        || control is Label
        || control is TextBox
        || control is ComboBox;
}

这篇关于如何确定控件是否具有Text属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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