Click here to Skip to main content
16,020,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...

C#
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;
}
Posted

Try that, you will know why :

C#
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;


On
SQL
richTextBoxColumns.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 :)
 
Share this answer
 
v2
Comments
rfresh 1-Jul-14 21:58pm    
I am using Courier New and I'm building the 'vertical line' marker on non-space characters...so I hope I will be OK. I will try it out tonight.

Thanks to both of you...
George Jonsson 1-Jul-14 22:25pm    
How does your solution work if there is more than 2 lines?
Nguyen.H.H.Dang 1-Jul-14 22:52pm    
@George Jonsson: I think the best way to let him know how to do things is show him what's wrong. That's how i learned programming too :)

About my solution, if he know what is wrong, he will know how to solve it :)
George Jonsson 1-Jul-14 22:59pm    
Well, the foreach loop he used was not suitable in this case. So that was one error.
Nguyen.H.H.Dang 1-Jul-14 23:20pm    
I dont like using foreach loop too, but if you really wanna use it, and textbox has more than 2 lines, this solution is good enough : (FONT = Courier New)

richTextBoxColumns.Text = " 1 2 3 4 5\r\n";
richTextBoxColumns.Text += "12345678901234567890123456789012345678901234567890\r\n";
richTextBoxColumns.Text += "12345678901234567890123456789012345678901234567890\r\n";
richTextBoxColumns.Text += "12345678901234567890123456789012345678901234567890\r\n";

int pos = 19;
int length = 0;
foreach (var line in richTextBoxColumns.Lines)
{
richTextBoxColumns.Select(length + pos, 1);
richTextBoxColumns.SelectionBackColor = Color.Yellow;
richTextBoxColumns.SelectionColor = Color.Red;
length += line.Length + 1;
}
This is one way to do it.

C#
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 as Courier 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?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900