在RichTextBox.Select与SubString方法之间不一致的行为 [英] Inconsistent behaviour between in RichTextBox.Select with SubString method

查看:242
本文介绍了在RichTextBox.Select与SubString方法之间不一致的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆