- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为彩色文本使用 RichTextBox。假设我想为文本的不同部分使用不同的颜色。到目前为止一切正常。
我目前遇到 RichTextBox 的 SelectionStart 属性问题。我已经为 RichTextBox 的 Text 属性设置了一些文本。如果文本包含 \r\n\r\n
,则 SelectionStart Position 将不匹配具有指定字符串的字符位置。
小例子(WinformsApplication.Form with a RichTextBox):
public Form1()
{
InitializeComponent();
String sentence1 = "This is the first sentence.";
String sentence2 = "This is the second sentence";
String text = sentence1 + "\r\n\r\n" + sentence2;
int start1 = text.IndexOf(sentence1);
int start2 = text.IndexOf(sentence2);
this.richTextBox1.Text = text;
String subString1 = text.Substring(start1, sentence1.Length);
String subString2 = text.Substring(start2, sentence2.Length);
bool match1 = (sentence1 == subString1); // true
bool match2 = (sentence2 == subString2); // true
this.richTextBox1.SelectionStart = start1;
this.richTextBox1.SelectionLength = sentence1.Length;
this.richTextBox1.SelectionColor = Color.Red;
this.richTextBox1.SelectionStart = start2;
this.richTextBox1.SelectionLength = sentence2.Length;
this.richTextBox1.SelectionColor = Color.Blue;
}
RichTextBox 看起来像这样:
如您所见,第二句话的前两个字符没有着色。这是 \r\n\r\n
产生的偏移量的结果。
这是什么原因?我应该使用另一个控件来为文本着色吗?如何以可靠的方式解决问题?我尝试用 String.Empty 替换 "\r\n\r\n"
,但这会产生其他偏移问题。
相关问题:
Inconsistent behaviour between in RichTextBox.Select with SubString method
最佳答案
似乎序列 \r\n
只在进行选择时才算作一个字符。您可以在所有 \r\n
都替换为 \n
的字符串副本中进行测量。
关于c# - RichTextBox SelectionStart 用换行符偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19293189/
这个问题在这里已经有了答案: selectionStart-End with textareas (2 个答案) 关闭 10 年前。 据我了解,selectionStart 必须从输入文本或文本区域
我正在尝试对文本框使用 SelectionStart 和 SelectionLength 属性。它没有效果,但也没有错误。它实际上是后台工作程序 ProgressChanged 方法的一部分,但我已经
我需要向用户显示,就像在 notepad.exe 中一样,他在光标上的文本的选择开始和长度。 选择长度没有问题,因为 Richtextbox 支持带有开始和结束的选择属性。 http://msdn.m
我正在为彩色文本使用 RichTextBox。假设我想为文本的不同部分使用不同的颜色。到目前为止一切正常。 我目前遇到 RichTextBox 的 SelectionStart 属性问题。我已经为 R
我在让下面的脚本与我的文本编辑器一起工作时遇到了一些困难。我不确定出了什么问题,但 selectionStart 和 selectionEnd 返回时未定义。 它应该在可编辑的 iframe 中获取突
在我的简单书签中,我调用了文档的所有 input 元素,然后尝试访问每个元素的 selectionStart: javascript: (function () { var inps=documen
我遇到了这个烦人的问题,我似乎无法在文本区域中获取所选文本的开始和结束索引,我得到的所有内容都是未定义的,如下所示: $('#myarea').selectionStart; // return un
我正在使用 selectionStart和 selectionEnd为了获得文本选择的起点和终点。 代码:https://codesandbox.io/s/busy-gareth-mr04o 但是,我
如果我输入 a然后 进入 IE11 文本区域并记录 selectionStart ,它是 2 (这是我所期望的)。但是,如果我以编程方式将 textarea 的值设置为 'a\n'相反,selecti
return layout.getLineForOffset(selectionStart); 这条线导致我的应用崩溃。 我正在用它来获取多行编辑文本中的当前光标行。像这样。 public int g
我正在尝试获取用户在文本框上单击的插入符号位置。我想在用户点击输入的位置添加一定的文本。我正在尝试在 input 的 onFocus 方法中执行此操作。但是,e.target.selectionSta
我正在尝试检测用户在关注 HTML 输入元素时点击的完整单词,然后查看它是否是 URL。我正在使用以下方法: let el = this.reminderInput.nativeElement var
每当我突出显示selectionStart = 0;时,该代码不执行。但是,如果我突出显示文本区域中第一个字符前面的一个字符,则代码将起作用。如果我突出显示文本区域中的第一个字符,有什么想法可以使此代
$(element)[0].selectionStart 似乎只适用于文本区域。是否有非文本区域的替代方案 我正在尝试将字符串包装在标记(粗体、斜体...)中的 DOM 元素中 另外,如何取消包装?
我有一个多行文本框,我想在文本框下方的标签中显示用户当前的行号和列号。看起来我可以使用 SelectionStart 属性、GetLineFromCharIndex 方法和 GetFirstCharI
我一直在努力使用 textarea 的 selectionStart 和 selectionEnd 属性,以使它们与 contenteditable div 元素一起使用。我在谷歌和SO上检查了很多相
我正在将一个 angularJS 文本插入器实现到一个文本区域,该文本区域将指定的文本插入到文本区域中光标的位置。经过一些谷歌搜索后,我在网上找到了这个示例。 指令的主体在这里 app.directi
使用这段代码,我可以检查 Firefox 中文本区域中的插入符位置: document.getElementById("myTextArea").selectionStart 这在 IE 8 中不起作
我有一个带有自定义样式的 TextBox 输入字段: 我添加了自定义样式:
如何根据鼠标位置设置 WinForms TextBox 的 SelectionStart?我正在扩展 TextBox 并希望在用户右键单击 TextBox 时将 carot 设置在鼠标点下方的位置(即
我是一名优秀的程序员,十分优秀!