在RichTextBox.Select与SubString方法之间不一致的行为 [英] Inconsistent behaviour between in RichTextBox.Select with SubString method
问题描述
我开发一个Windows窗体应用程序。我使用 RichTextBox.LoadFile
方法从文件加载文本和突出显示文本的某些部分。该文件中的文本包含返回字符。
I am developing a Windows Form application. I use RichTextBox.LoadFile
method to load text from a file and highlight some portion of the text. The text in the file contains return characters.
假设我想强调高亮
。首先,我找到的startIndex
和长度
的高亮的
部分,然后用 RichTextBox.Select(的startIndex,长度)
,给点颜色吧。
Suppose that I want to highlight the highlight
. First I find the startIndex
and the length
of the the highlight
part, then use RichTextBox.Select(startIndex, length)
and give some color to it.
当我使用子
的方法,我可以找到正确的文本。但是,当我申请的startIndex
的相同的值,长度
到选择
方法,突出显示部分变成 [空格] [空格] [空格]的highli
。它的外观选择
方法需要一些返回字符考虑,并会导致一些问题。
When I use SubString
method, I can find the correct text. But when I apply the same value of startIndex
and length
to Select
method, the highlighted part becomes [space][space][space]the highli
. It looks the Select
method takes some return characters into account and cause some problem.
我怎样才能解决这个问题?
How can I resolve it?
推荐答案
我认为你应该使用查找()
的方法的RichTextBox
:
I think you should use the Find()
method of RichTextBox
:
int nextStartIndex;
public void Find(string keyword){
int i = richTextBox1.Find(keyword, nextStartIndex, RichTextBoxFinds.None);
if(i != -1) {
nextStartIndex = i + keyword.Length;
}
}
这篇关于在RichTextBox.Select与SubString方法之间不一致的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!