问题
In my program after clicking on the button - selected ListView entries should be copied to RichTextBox. ListView contains contact information, and the effect I want to accomplish is similar to the one in Oultook (when choosing contacts from contact book). Part of my code that serves this purpose looks like that:
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 FontStyle is inherited by entire text and everything I enter after entry from ListView is also underlined.
So my question is - how to underline only certain text (where I have made a mistake)?
There is similar topic on stackoverflow here unfortunately in my case solution stated there will be waste of resources.
回答1:
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;
Total hack, but basically, if you select the text after it's all there, but don't select ALL of it (which is why I add the extra " ") and then set the selection text, it has the desired effect.
回答2:
The problem with your code is that the SelectionFont is just that -- the font of the selection. If there is no selection, the font change isn't going to do anything. The solution that BFree offered seems like it would work. That's the same thing that I would do if I was typing the document in WORD -- I'd add some characters after the underlined section before I underlined it so the extra characters would "save" the original formatting for when I continued the document.
+1 for BFree, but I don't have a reputation yet :( ...
回答3:
Before adding more text to the end of the text box, position the cursor at the end, and set the font to the desired style. Then a call to rtb.AppendLine() should produce the desired results.
It's key to remember that the RTB control performs in the same manner as any other word processor. You set a style and start typing. Then anything you type after setting that style will take on those attributes that are under the corsor.
Update: This appears to work perfectly.
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
来源:https://stackoverflow.com/questions/821347/how-to-selectively-underline-strings-in-richtextbox