多行上的richTextBox SelectionBackColor [英] richTextBox SelectionBackColor on multiple lines
问题描述
我正在尝试使用红色前景色(SelectionColor)构建黄色BG颜色的垂直线参考。
我目前只有两行文字我的richTextBox。
但只有第一行颜色正确。第二行没有颜色。如何在每行20号的每个字符上着色(因此它显示一条垂直的红线)。
感谢您的帮助...
richTextBoxColumns.Text = 1 2 3 4 5 \\ n;
richTextBoxColumns.Text + = 12345678901234567890123456789012345678901234567890;
foreach ( var line in richTextBoxColumns.Lines)
{
richTextBoxColumns.Select( 19 , 1 ) ;
richTextBoxColumns.SelectionBackColor = Color.Yellow;
richTextBoxColumns.SelectionColor = Color.Red;
}
试试看,你会知道原因:
richTextBoxColumns.Text = 1 2 3 4 5\\\\ N跨度>;
richTextBoxColumns.Text + = 12345678901234567890123456789012345678901234567890;
richTextBoxColumns.Select( 19 , 1 );
richTextBoxColumns.SelectionBackColor = Color.Yellow;
richTextBoxColumns.SelectionColor = Color.Red;
int line1Length = richTextBoxColumns.Lines [ 0 ]。长度;
richTextBoxColumns.Select(line1Length + 19 , 1 );
richTextBoxColumns.SelectionBackColor = Color.Yellow;
richTextBoxColumns.SelectionColor = Color.Red;
OnrichTextBoxColumns。选择(line1Length + 19 , 1 );
只需按照您的意愿更改+ 19部分。
调试此代码,你会发现,你的字体长度有问题:)
这是一种方法。
< span class =code-keyword> int markIndex = 0 ;
int startIndex = 0 ;
do
{
markIndex = startIndex + 19 ;
richTextBoxColumns.Select(markIndex, 1 );
richTextBoxColumns.SelectionBackColor = Color.Yellow;
richTextBoxColumns.SelectionColor = Color.Red;
startIndex = richTextBoxColumns.Text.IndexOf(' \ n',startIndex + 1 跨度>);
} while (startIndex > = 0 );
richTextBoxColumns.SelectedText = ;
你将遇到的问题是你需要使用字符间距相等的字体,例如Courier New
,否则你的垂直线将小于笔直。
白色空间似乎仍然是一个问题。
其他任何有解决方案的人是什么?
I'm trying to build a vertical line reference of yellow BG color with red foreground color (SelectionColor).
I currently only have two lines of text in my richTextBox.
But only the first line colors correctly. There is no color in the second line. How can I color each char at col 20 in each line (so it shows a vertical red line).
Thank you for any help...
richTextBoxColumns.Text = " 1 2 3 4 5\r\n";
richTextBoxColumns.Text += "12345678901234567890123456789012345678901234567890";
foreach (var line in richTextBoxColumns.Lines)
{
richTextBoxColumns.Select(19,1);
richTextBoxColumns.SelectionBackColor = Color.Yellow;
richTextBoxColumns.SelectionColor = Color.Red;
}
Try that, you will know why :
richTextBoxColumns.Text = " 1 2 3 4 5\r\n"; richTextBoxColumns.Text += "12345678901234567890123456789012345678901234567890"; richTextBoxColumns.Select(19, 1); richTextBoxColumns.SelectionBackColor = Color.Yellow; richTextBoxColumns.SelectionColor = Color.Red; int line1Length = richTextBoxColumns.Lines[0].Length; richTextBoxColumns.Select(line1Length + 19, 1); richTextBoxColumns.SelectionBackColor = Color.Yellow; richTextBoxColumns.SelectionColor = Color.Red;
OnrichTextBoxColumns.Select(line1Length + 19, 1);
Just change the "+ 19" part as your wish.
Debug this code and you will find that, your Font length has problem :)
This is one way to do it.
int markIndex = 0; int startIndex = 0; do { markIndex = startIndex + 19; richTextBoxColumns.Select(markIndex, 1); richTextBoxColumns.SelectionBackColor = Color.Yellow; richTextBoxColumns.SelectionColor = Color.Red; startIndex = richTextBoxColumns.Text.IndexOf('\n', startIndex + 1); } while (startIndex >= 0) ; richTextBoxColumns.SelectedText = "";
The problem you will have is that you need to use a font with equal character spacing, such asCourier New
, otherwise your vertical line will be less than straight.
white space still seems to be a problem, though.
Anyone else that have a solution for that?
这篇关于多行上的richTextBox SelectionBackColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!