Getting text from a RichTextBox

Ask general questions here.
JimLin
Posts: 70
Joined: Mon Jun 02, 2014 4:23 pm

Getting text from a RichTextBox

Post by JimLin » Wed Apr 12, 2023 3:39 pm

Hi,

One of the elements in our AUT has changed from a standard text box to a RichTextBox, which is constructed in a completely different way to a standard textbox. Because of this change, I am now really struggling to get the text back to validate against.

The text element now contains the string below and the 50.00 is the text that I am trying to validate against.
RichTextBox text string.png
I have a working RexEx example to get the text back ( <Paragraph>(\d{1,3}(?:\.\d{2})?)<\/Paragraph> ), but the devs say that this will be fragile due to continual changes/updates to .net.

I have hunted around the web for a C# solution and there is limited help for this issue.

One of our devs pointed me at this code, but I have not been able to get it working, as I have been unable to locate which assembly 'TextRange' and 'Document' live under:

Code: Select all

string StringFromRichTextBox(RichTextBox rtb)
{
    TextRange textRange = new TextRange(
        // TextPointer to the start of content in the RichTextBox.
        rtb.Document.ContentStart,
        // TextPointer to the end of content in the RichTextBox.
        rtb.Document.ContentEnd
    );

    // The Text property on a TextRange object returns a string
    // representing the plain text content of the TextRange.
    return textRange.Text;
}
I have also found this snippet, but have been unable to get it working. The suggested assembly is Ranorex.Core. but that has not worked either.

Code: Select all

// Get the RichTextBox control using Ranorex Studio's repository
var richTextBox = repo.MyForm.MyRichTextBox;

// Create a TextAdapter instance for the RichTextBox control
var textAdapter = new TextAdapter(richTextBox);

// Get the text content of the RichTextBox control
var text = textAdapter.GetText();

// Print the text content to the console
Console.WriteLine(text);
The main problem is that my coding skills are pretty limited and this is proving to be a real struggle for me.

Any help or suggestions would be appreciated.

Cheers,
James

Ranorex version: 10.5.4
.net version: 7
Windows 10
You do not have the required permissions to view the files attached to this post.