本文介绍了遍历 C# Winform 控件时,我无法在富文本框控件上使用 Rtb 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些富文本格式 (RTF) 文本放入富文本框 (RTB).我正在迭代几个 winform 控件以获取我需要的 RTB.我可以使用 RichTextBox.Text 属性向文本框添加普通文本,但是当我尝试使用 RichTextBox.Rtb 属性向其中添加 RTF 文本时,我收到错误消息,指出该控件不存在该文本(控件不包含 Rtb 的定义).在下面的代码示例中,为什么我的rtbControl"没有即使控件应该是 RichTextBox 也有 Rtb 属性吗?如何使用 Rtb 属性/为此控件设置 RTF 文本?

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
                }
            }

   

推荐答案

在 if 语句中,它检查 rtbControl 是否是 RichTextBox.如果是,您需要创建一个新的 RichTextBox 变量才能使用 RichTextBox 属性,例如 RtfSelectedRTF.

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;
   }

这篇关于遍历 C# Winform 控件时,我无法在富文本框控件上使用 Rtb 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 02:46