This is a migrated thread and some comments may be shown as answers.

Unable to Retrieve text from "RichTextBox".

3 Answers 109 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
sivaraj
Top achievements
Rank 1
sivaraj asked on 19 Aug 2011, 03:38 PM
Hi,

I am not able to Retrieve text from "RichTextBox". While using the following piece of code it returns Empty string.

ActiveBrowser.Find.AllByType<RichTextBox>()[0].Text

and the following code also returns Empty string.

ActiveBrowser.Find.AllByType<RichTextBox>()[0].TextContent

The version of Framework would be "2011.1.609.0"

Thanks in advance,
Siva

3 Answers, 1 is accepted

Sort by
0
Cody
Telerik team
answered on 19 Aug 2011, 10:29 PM
Hello Sivaraj,

The Silverlight RichTextBox is tricky to work with. This control is a "container" type of control meaning it contains other "block" elements, usually the Paragraph or Section. Its these block elements that hold the actual text displayed in the RichTextBox. Thus to fetch the text that is being displayed you must enumerate through the individual blocks and fetch the text from each block element.

To make matters worse the contents of the RichTextBox are not contained in the Silverlight Visual Tree. You have to get the Xaml property and parse the contents yourself. Let's look at a real example... let's assume that you have this in the XAML file for your Silverlight application:

<RichTextBox AutomationProperties.AutomationId="richTextBox1" Name="richTextBox1" Margin="310,6,6,235">
    <Paragraph FontSize="11" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left">
        <Run FontWeight="Bold" Text="Now is the time for all good men to come to the aid of their country." />
    </Paragraph>
</RichTextBox>

Running your application will show text like that shown in the attached screenshot. However in DOM Explorer all you see is this:

<?xml version="1.0"?>
<richtextbox Name="richTextBox1" AutomationId="richTextBox1" Uid="16157963">
  <grid Name="RootElement" Uid="37840511">
    <border Name="Border" Uid="34030663">
      <border Name="MouseOverBorder" Uid="33607346">
        <scrollviewer Name="ContentElement" AutomationId="ContentElement" Uid="63386473">
          <border Uid="54749715">
            <grid Uid="35909463">
              <scrollcontentpresenter Name="ScrollContentPresenter" Uid="5020285">
                <richtextboxview BaseType="RichTextBoxView" Uid="22985394" />
              </scrollcontentpresenter>
              <rectangle Uid="26673201" />
              <scrollbar Name="VerticalScrollBar" AutomationId="VerticalScrollBar" Uid="45182569" />
              <scrollbar Name="HorizontalScrollBar" AutomationId="HorizontalScrollBar" Uid="3989940" />
            </grid>
          </border>
        </scrollviewer>
      </border>
    </border>
    <border Name="DisabledVisualElement" Uid="38732217" />
    <border Name="FocusVisualElement" Uid="13045638" />
  </grid>
</richtextbox>

Notice that there's no text in the DOM view even though it is specified in the XAML and even displayed in the application. We have to use code to fetch the XAML that the RichTextBox holds as data then parse out the text buried in it and do whatever we want from there. Here's an example:

// Fetch the XAML property of the RichTextBox
string rtbContents = (string)Pages.SilverlightAppTesting.SilverlightApp.RichTextBox1Richtextbox.GetProperty(new AutomationProperty("Xaml", typeof(string)));
// Load it into an XmlDocument for easier parsing
XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(rtbContents);
// Find all "Run" nodes as this is where the text is buried
XmlNodeList runNodes = doc.GetElementsByTagName("Run");
// Display the Text attribute of each run node
foreach(XmlNode runNode in runNodes)
{
    Log.WriteLine(runNode.Attributes["Text"].Value);
}



Kind regards,
Cody
the Telerik team
Vote for Telerik Test Studio at the Annual Automation Honors Voting!
0
Andrey Cherepakha
Top achievements
Rank 1
answered on 01 Mar 2012, 10:24 AM
Hello!

Thanks alot for the great example!

I used it with WebAii with 2011.1.712.0 and it works fine. But after migration to WebAii 2011.2.1413.0 it does not work.

I have following code

public string GetText(int nodeIndex) 
{  
    RichTextBox local = _app.Find.ByName<RichTextBox >("RichTextBoxName"); 
    string rtbContents = (string)local.GetProperty(new AutomationProperty("Xaml", typeof(string))); 
    XmlDocument doc = new System.Xml.XmlDocument(); 
    doc.LoadXml(rtbContents); 
    XmlNodeList runNodes = doc.GetElementsByTagName("Run"); 
    return runNodes[nodeIndex].Attributes["Text"].Value; 
}

Before (WebAii 2011.1.712.0) runNodes contained several elements. For now (WebAii 2011.2.1413.0) it contains only one element.
Was something changed in RichTextBox handling ?

Should I mention that we migrated our application from Silverlight 4 to Silverlight 5. May be the reason is the version of Silverlight?

Thanks.
0
Andrey Cherepakha
Top achievements
Rank 1
answered on 02 Mar 2012, 03:53 PM
I have investigated the content of RichTextBox.Xaml property and found out that by some reason it does not contain Text attribute any more. In my case it looks like that (in the example I deleted all the attributes of Paragraph and Run tags):

<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Paragraph>
        <Run>Order ID: </Run>
        <Run>48214</Run>
    </Paragraph>
    <Paragraph>
        <Run>Order Status: </Run>
        <Run>Submitted</Run>
    </Paragraph>
</Section>

So, in my code presented below, I just replaced the last line with

return runNodes[nodeIndex].InnerText;

Now it works for me.
Tags
General Discussions
Asked by
sivaraj
Top achievements
Rank 1
Answers by
Cody
Telerik team
Andrey Cherepakha
Top achievements
Rank 1
Share this question
or