0

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.

haynar
  • 5,961
  • 7
  • 33
  • 53
  • 2
    I've never worked with this **at all** but can't you write to the textbox using the automation framework, read the contents and if it's the value you wrote, it means it's not readonly? – Only Bolivian Here Jul 10 '12 at 17:24
  • 2
    @SergioTapia You *could*, but what if it's not read only? Now you've overwritten user data! To avoid that, you'd have to read the data first, try a write, and then restore it. And what if it *becomes* read only after the test write but before the restore? You're just asking for trouble. – dlev Jul 10 '12 at 17:28
  • @dlev: I assume that he's testing the automation on test bed environments, not actual users running the app. I do agree however, with certain scenarios such as the one you mentioned where writing once, then locking. Maybe this isn't considered by OP? – Only Bolivian Here Jul 10 '12 at 17:31

2 Answers2

4

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
}
dlev
  • 48,024
  • 5
  • 125
  • 132
0

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");
                }
            }
        }
Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48