C# 如何在 RichTextBox 中选择性地给字符串加下划线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/821347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to selectively underline strings in RichTextBox?
提问by kyrisu
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:
单击按钮后,在我的程序中 - 选定的 ListView 条目应复制到 RichTextBox。ListView 包含联系人信息,我想要实现的效果和Oultook 中的类似(从通讯录中选择联系人时)。用于此目的的部分代码如下所示:
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.
不幸的是,FontStyle 以某种方式被整个文本继承,我从 ListView 输入后输入的所有内容也带有下划线。
So my question is - how to underline only certain text (where I have made a mistake)?
所以我的问题是 - 如何只在某些文本下划线(我犯错的地方)?
There is similar topic on stackoverflow hereunfortunately in my case solution stated there will be waste of resources.
有类似的话题在计算器这里不幸的是在我的情况的解决方案表示会有资源浪费。
采纳答案by BFree
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.
完全黑客,但基本上,如果您在文本全部存在之后选择文本,但不要选择所有文本(这就是我添加额外的“”的原因),然后设置选择文本,它具有所需的效果。
回答by Sam Axe
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.
在文本框的末尾添加更多文本之前,将光标定位在末尾,并将字体设置为所需的样式。然后调用 rtb.AppendLine() 应该会产生所需的结果。
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.
重要的是要记住 RTB 控件的执行方式与任何其他文字处理器相同。您设置样式并开始打字。然后,您在设置该样式后键入的任何内容都将采用 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
回答by JPhi1618
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.
您的代码的问题在于 SelectionFont 就是——选择的字体。如果没有选择,字体更改不会做任何事情。BFree 提供的解决方案似乎可行。如果我在 WORD 中输入文档,我会做同样的事情——我会在加下划线之前在带下划线的部分之后添加一些字符,这样多余的字符会“保存”我继续输入文档时的原始格式。
+1 for BFree, but I don't have a reputation yet :( ...
BFree +1,但我还没有声誉:( ...