Domanda

I am developing an application to automates other applications. I want to be able to determine whether the textbox element on the "other" application is readonly or not. In case of one-line textboxes MS UI Automation framework provides ValuePattern and I can get readonly attribute from that pattern, but when we have multiline textbox, there is no ValuePattern available and I only can access TextPattern and ScrollPattern. How can I get the readonly attribute from multiline textbox using MS UI Automation?

P.S. I've tried to find something about this on the internet but it seems that there are no so much information about MS UI Automation in general.

È stato utile?

Soluzione

The TextPattern pattern provides a way to check ranges for read-only status. Checking the full DocumentRange tells you if the entire textbox is read-only:

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
}

Altri suggerimenti

For example to check if textBox2 is read only.

The method to check if textBox2 is read only:

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

Using the method on button click event:

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

To check all textboxes on a form if read only or not using the same method:

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");
                }
            }
        }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top