title: MS UI Automation - How to get text from ControlType.text
body: <p>I have a small windows application that has a series of labels on it. This application will be globalized and there is a possibility that the text on these labels might get truncated. I am trying to automate to identify truncated text on these labels.</p>
  
  <p>For other controls, I can use TextPattern.Pattern through which I can find the visible text and the actual text inside the control. But for the labels (ControlType.text) the TextPattern is not supported. How do I find the visible text for these lables using UI automation.</p>
  
  <p>Here is the code I tried. If I pass control type as Document it works. But with Text control type it gives a unsupported pattern exception.</p>
  
  <pre><code>private String TextFromSelection(AutomationElement target, Int32 length)
          {
              // Specify the control type we're looking for, in this case 'Document'
              PropertyCondition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text);
  
              // target --&gt; The root AutomationElement.
              AutomationElement textProvider = target.FindFirst(TreeScope.Descendants, cond);
  
              TextPattern textpatternPattern = textProvider.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
  
              if (textpatternPattern == null)
              {
                  Console.WriteLine("Root element does not contain a descendant that supports TextPattern.");
                  return null;
              }
  
  
              var test = textpatternPattern.DocumentRange.GetText(-1).TrimEnd('\r');
  
              var tpr = textpatternPattern.GetVisibleRanges();
              var txt = tpr[0].GetText(-1);
  
              return txt;
          }
  </code></pre>