【问题标题】:How to selectively underline strings in RichTextBox?如何有选择地在 RichTextBox 中为字符串加下划线?
【发布时间】:2009-05-06 02:23:23
【问题描述】:

在我的程序中单击按钮后 - 应将选定的 ListView 条目复制到 RichTextBox。 ListView 包含联系人信息,我想要完成的效果类似于 Oulook 中的效果(从通讯录中选择联系人时)。用于此目的的部分代码如下所示:

    private void toButton_Click(object sender, EventArgs e)
    {
        int start = 0;
        for (int i = 0; i < contactsListView.SelectedItems.Count; i++)
        {
            if (contactsTextBox.TextLength != 0) contactsTextBox.Text += "; ";
            start = contactsTextBox.TextLength;
            contactsTextBox.Text += contactsListView.SelectedItems[i].Text + " " + contactsListView.SelectedItems[i].SubItems[1].Text + " [" + contactsListView.SelectedItems[i].SubItems[2].Text + "]";
            contactsTextBox.Select(start, contactsTextBox.TextLength);       
            contactsTextBox.SelectionFont = new Font(contactsTextBox.SelectionFont, FontStyle.Underline);
            contactsTextBox.DeselectAll();
            contactsTextBox.SelectionFont = new Font(contactsTextBox.SelectionFont, FontStyle.Regular);
        }
    }

不幸的是,整个文本都继承了 FontStyle,我从 ListView 输入后输入的所有内容也都带有下划线。

所以我的问题是 - 如何仅在某些文本下划线(我犯了错误)?

在 stackoverflow here 上有类似的主题,不幸的是,在我的案例中,解决方案声明会浪费资源。

【问题讨论】:

    标签: c# richtextbox rtf


    【解决方案1】:

    试试这个:

            int start = 0;
            for (int i = 0; i < contactsListView.SelectedItems.Count; i++)
            {
                if (contactsTextBox.TextLength != 0) contactsTextBox.Text += "; ";
                start = contactsTextBox.TextLength;
                contactsTextBox.Text += contactsListView.SelectedItems[i].Text +" " + contactsListView.SelectedItems[i].SubItems[1].Text + " [" + contactsListView.SelectedItems[i].SubItems[2].Text + "]";
            }
    
    
            this.contactsTextBox.Text += " ";
            this.contactsTextBox.SelectionStart = 0;
            this.contactsTextBox.SelectionLength = this.contactsTextBox.Text.Length-1;
            contactsTextBox.SelectionFont = new Font(contactsTextBox.SelectionFont, FontStyle.Underline);
            this.contactsTextBox.SelectionLength = 0;
    

    Total hack,但基本上,如果您在文本全部存在之后选择文本,但不要选择全部(这就是我添加额外的“”的原因)然后设置选择文本,它具有预期的效果.

    【讨论】:

      【解决方案2】:

      您的代码的问题在于 SelectionFont 就是这样 - 选择的字体。如果没有选择,字体更改将不会做任何事情。 BFree 提供的解决方案似乎可行。如果我在 WORD 中键入文档,我会做同样的事情——我会在下划线部分之后添加一些字符,然后再添加下划线,这样额外的字符就会“保存”原始格式,以便在我继续文档时使用。

      +1 为 BFree,但我还没有声望 :( ...

      【讨论】:

        【解决方案3】:

        在文本框末尾添加更多文本之前,请将光标放在末尾,并将字体设置为所需的样式。然后调用 rtb.AppendLine() 应该会产生所需的结果。

        重要的是要记住 RTB 控件的执行方式与任何其他文字处理器相同。您设置样式并开始输入。然后,您在设置该样式后键入的任何内容都将采用 corsor 下的那些属性。

        更新: 这似乎工作得很好。

        Dim tTexts() As String = {"Dont underline me", "Underline me", "Dont underline me", "Underline me", "Dont underline me", "Underline me", "Dont underline me", "Underline me", "Dont underline me", "Underline me", "Dont underline me", "Underline me"}
            Dim tUnderline As Boolean = False
            Dim tIndex As Integer = 0
        
            With oRTB
                For tIndex = tTexts.GetLowerBound(0) To tTexts.GetUpperBound(0)
                    If tUnderline Then
                        .SelectionStart = .Text.Length
                        .SelectionFont = New Font("Arial", 12, FontStyle.Underline)
                    Else
                        .SelectionStart = .Text.Length
                        .SelectionFont = New Font("Arial", 12, FontStyle.Regular)
                    End If
                    .AppendText(tTexts(tIndex))
                    tUnderline = Not tUnderline
                Next
            End With
        

        【讨论】:

        • 你能解释一下吗?我可以通过 contactsTextBox.SelectionStart = contactsTextBox.TextLength > 0 将光标设置在末尾?联系人文本框.文本长度:0;然后我通过 contactsTextBox.SelectionFont = new Font(contactsTextBox.SelectionFont,FontStyle.Regular); 设置选择字体但仍然所有内容都带有下划线:/我尝试通过 AppendText 方法添加文本(不走运)。
        猜你喜欢
        • 2014-09-26
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 2019-03-19
        • 2014-10-14
        • 1970-01-01
        • 2021-01-01
        • 2019-11-02
        相关资源
        最近更新 更多