如何有选择地强调在RichTextBox的字符串? [英] How to selectively underline strings in RichTextBox?
问题描述
在点击按钮后,我的程序 - 选定的ListView项应该被复制到RichTextBox中。 ListView控件包含联系信息,我想要完成的效果类似于一个在Oultook(选择从通讯录联系人时)。我的代码是服务于这一目的的部分看起来像:
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);
}
}
不幸的是不知何故fontstyle的是整个文本和一切我继承从ListView的条目还强调后进入
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.
推荐答案
试试这个:
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.
这篇关于如何有选择地强调在RichTextBox的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!