0

I am trying to put some Rich Text Formatted (RTF) text into a Rich Text Box (RTB). I am iterating through several winform controls to grab the RTB I need. I can use the RichTextBox.Text property to add normal text to the text box, however when I try to use the RichTextBox.Rtb property to add RTF text to it I get an error saying it doesn't exist for that control ("Control does not contain a definition for Rtb). In the code example below, why doesn't my "rtbControl" have the Rtb property even though the control should be a RichTextBox? How can I use the Rtb property / set RTF text for this control?

    // RTF string I want to display in the RTB
    string some_rtb_text = @"{\rtf1\ansi This is some \b bold\b0 text.}";  
            foreach (Control rtbControl in GlobalVars.myUserControl1.Controls)  // iterate through all the controls and find the one I want
            {
                if (rtbControl is RichTextBox & rtbControl.Name == "the_text_box_I_want_to_use")  // Making sure the control is a RichTextBox
                {
                    rtbControl.Rtb = some_rtb_text;  // it's telling me that rtbControl does not contain a definition for Rtb
                }
            }

   
Braxvan
  • 67
  • 14

2 Answers2

2

Rhys Wootton's answer is correct and IMHO answers your question, but can be simplified a bit:

if (rtbControl.Name == "name_of_control" && rtbControl is RichTextBox rtb)
{
    rtb.Rtf = some_rtb_text;
}

Steven Gann
  • 174
  • 10
  • 1
    It is, I just tested it! He edited it because the earlier version did not answer the question. Thanks for the simplification! – Braxvan Jul 31 '20 at 20:32
  • 1
    Didn't realize you could simplify it like that, cool stuff. Thanks for adding that! – Rhys Wootton Jul 31 '20 at 20:56
1

In the if statement it checks if rtbControl is a RichTextBox. If it is, you need to create a new RichTextBox variable to be able to use RichTextBox properties such as Rtf or SelectedRTF.

if (rtbControl is RichTextBox & rtbControl.Name == "name_of_control")  // Making sure the control is a RichTextBox
   {
        RichTextBox rtb = rtbControl as RichTextBox;
        rtb.Rtf = some_rtb_text;
   }
Rhys Wootton
  • 182
  • 8
  • There is a big difference between rtbControl.Rtb and rtbControl.text though. Using .text will just add that string as plain text. Using .Rtb should add the text as a properly formatted RTF string. I need it to read the rich text format properly, and plain old .text doesn't do that. – Braxvan Jul 31 '20 at 20:10
  • 1
    Ah, sorry about that. I've changed it now so it should work. However I tried it with your RTF text and it didn't work, but did with an example I found online, so double check that. I'm not too knowledgeable on RTF. – Rhys Wootton Jul 31 '20 at 20:17
  • You're good, the edited version is what I was looking for! Thank you so much for your help. You're right, that RTF text I have up there does not work, i'll update that with some working RTF text. – Braxvan Jul 31 '20 at 20:35
  • 1
    No problem! I'll have to look more into RTF now, it has got me intrigued. – Rhys Wootton Jul 31 '20 at 20:58