【问题标题】:How to find whether the TextBox is readonly or not如何查找 TextBox 是否为只读
【发布时间】:2016-05-21 18:59:44
【问题描述】:

我正在开发一个应用程序来自动化其他应用程序。我希望能够确定“其他”应用程序上的文本框元素是否是只读的。如果是单行文本框,MS UI 自动化框架提供了 ValuePattern,我可以从该模式中获取只读属性,但是当我们有多行文本框时,没有可用的 ValuePattern,我只能访问 TextPattern 和 ScrollPattern。如何使用 MS UI 自动化从多行文本框中获取只读属性?

附:我试图在互联网上找到有关此的一些信息,但似乎没有太多关于 MS UI 自动化的信息。

【问题讨论】:

  • 我从来没有使用过这个根本但是你不能使用自动化框架写入文本框,阅读内容,如果它是你写的值,它意味着它不是只读的?
  • @SergioTapia 你可以,但如果它不是只读的呢?现在您已经覆盖了用户数据!为避免这种情况,您必须先读取数据,尝试写入,然后再恢复。如果它在测试写入之后但在恢复之前变为只读呢?你只是自找麻烦。
  • @dlev:我假设他正在测试台环境中测试自动化,而不是运行应用程序的实际用户。但是,我确实同意某些情况,例如您提到的写一次,然后锁定的情况。也许这不是 OP 考虑的?

标签: c# .net microsoft-ui-automation


【解决方案1】:

TextPattern 模式提供了一种检查范围的只读状态的方法。检查完整的DocumentRange 会告诉您整个文本框是否为只读:

TextPattern textPattern = textProvider.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

object roAttribute = textPattern.DocumentRange.GetAttributeValue(TextPattern.IsReadOnlyAttribute);
if (roAttribute != TextPattern.MixedAttributeValue)
{
    bool isReadOnly = (bool)roAttribute;
}
else
{
    // Different subranges have different read only statuses
}

【讨论】:

    【解决方案2】:

    例如检查textBox2 是否为只读。

    判断textBox2是否只读的方法:

    private bool checkReadOnly(Control Ctrl)
            {
                bool isReadOnly = false;
                if(((TextBox)Ctrl).ReadOnly == true)
                {
                    isReadOnly = true;
                }
                else
                {
                    isReadOnly = false;
                }
                return isReadOnly;
            }
    

    在按钮点击事件上使用方法:

    private void button1_Click(object sender, EventArgs e)
            {
                if (checkReadOnly(textBox2) == true)
                {
                    MessageBox.Show("textbox is readonly");
                }
                else
                {
                    MessageBox.Show("not read only textbox");
                }
            }
    

    使用相同的方法检查表单上的所有textboxes是否只读:

    private void button2_Click(object sender, EventArgs e)
            {
                foreach(Control ct in Controls.OfType<TextBox>())
                {
                    if (checkReadOnly(ct) == true)
                    {
                        MessageBox.Show(ct.Name + " textbox is readonly");
                    }
                    else
                    {
                        MessageBox.Show(ct.Name + " not read only textbox");
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 2019-09-26
      • 1970-01-01
      相关资源
      最近更新 更多