How to selectively underline lines in RichTextBox?

In my program, after clicking the button, the selected ListView entries should be copied to the RichTextBox. The ListView contains contact information and the effect I want to perform is the same as in Oultook (when selecting contacts from a contact book). The part of my code that serves this purpose looks like this:

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

      

Unfortunately somehow the FontStyle is inherited by the whole text and everything I entered after entering from the ListView is also underlined.

So my question is, how do I underline only certain text (where did I go wrong)?

There is a similar topic on stackoverflow here , unfortunately in my case the solution is stated to waste resources.

+1


source to share


3 answers


Try this instead:

        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;

      



A general hack, but basically if you select the text after it there, but don't select ALL of it (which is why I add an extra "") and then set the highlight text, it has the desired effect.

+3


source


Before adding more text to the end of the text box, place your cursor at the end and set the font to the style you want. Then calling rtb.AppendLine () should give the desired results.

It's important to remember that RTB control works just like any other word processor. You set the style and start typing. Then whatever you type after setting this style will take on those attributes that are under the corsor.



Update: This seems to work fine.

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

      

0


source


The problem with your code is that the SelectionFont is exactly the selection font. If there is no choice, changing the font will do nothing. The solution provided by BFree looks like it will work. The same thing that I would do if I were typing the document in WORD - I would add some characters after the underlined section before underlining it so that the additional characters "preserve" the original formatting as I continued the document.

+1 for BFree, but I don't have a reputation yet: (...

0


source







All Articles