Change the color of text at a specific location on each text line

G

Guest

I want to change the font color and weight at a specific position in the text.

I am not sure where or what I need to do. It will be at position 155/156
from the left on each line.

Here is the code I need to put it into.

using System;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
bool firstLine = true;
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
int txPos;
int rxPos = -1;
if (firstLine)
firstLine = false;
else
sb.Append("\r\n");
if (((txPos = line.IndexOf("TX")) > -1) || ((rxPos =
line.IndexOf("RX")) > 0))
{
int charactersTillPoint;
if (txPos > -1)
charactersTillPoint = txPos;
else
charactersTillPoint = rxPos;
string part0 = line.Substring(0, charactersTillPoint);
string part1 = line.Substring(charactersTillPoint);
sb.Append(part0.PadRight(86));
sb.Append(part1);
}
else
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}

Thanks,
 
N

Nicholas Paldino [.NET/C# MVP]

Brian,

What are you displaying this in? All you have shown is some content,
but not the mechanism by which you want to display it.
 
G

Guest

I have a text file that I modify from a compressed hex format into a single
line. This is the output of that part of the program that displays in a
RichTextBox;

2007/07/27 11:59:37 [123007]ARES_EINDICATION 010.050.016.004 407.3.01
(65e1) RX 68 bytes 65 E1 26 02 A4 5C AA 20 76 79 51 23 50 76 37 62
99 00 48 02 02 C7 88 01 C7 88 AA 50 76 37 62 99 20 76 79 51 23 D7 07 07 1B 0B
3B 22 00 00 10 06 0A 06 06 06 06 06 06 06 0A 0A 06 06 06 0A 0A BB 1C 78 56 BC

In this sample the hex pair 48 represents the position in the line. It lays
out at position 155/156 from the left. I need to change this to BLUE/BOLD for
every instance of the ARES_INIDICATION and ARES_EINDICATION as the form loads.


Hope that clears it up.

Thanks,


Nicholas Paldino said:
Brian,

What are you displaying this in? All you have shown is some content,
but not the mechanism by which you want to display it.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian Cook said:
I want to change the font color and weight at a specific position in the
text.

I am not sure where or what I need to do. It will be at position 155/156
from the left on each line.

Here is the code I need to put it into.

using System;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
bool firstLine = true;
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
int txPos;
int rxPos = -1;
if (firstLine)
firstLine = false;
else
sb.Append("\r\n");
if (((txPos = line.IndexOf("TX")) > -1) || ((rxPos =
line.IndexOf("RX")) > 0))
{
int charactersTillPoint;
if (txPos > -1)
charactersTillPoint = txPos;
else
charactersTillPoint = rxPos;
string part0 = line.Substring(0,
charactersTillPoint);
string part1 = line.Substring(charactersTillPoint);
sb.Append(part0.PadRight(86));
sb.Append(part1);
}
else
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}

Thanks,
 
N

Nicholas Paldino [.NET/C# MVP]

Brian,

What you will want to do in this case is for the sections that you want
to change the font/color of, you would select them using the
SelectedText/SelectedRtf properties, or the SelectionLength/SelectionStart
properties. Once you have the text selected that you want to format, you
can set the SelectionColor and SelectionFont properties to change the
font/color of the selected text.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian Cook said:
I have a text file that I modify from a compressed hex format into a single
line. This is the output of that part of the program that displays in a
RichTextBox;

2007/07/27 11:59:37 [123007]ARES_EINDICATION 010.050.016.004 407.3.01
(65e1) RX 68 bytes 65 E1 26 02 A4 5C AA 20 76 79 51 23 50 76 37
62
99 00 48 02 02 C7 88 01 C7 88 AA 50 76 37 62 99 20 76 79 51 23 D7 07 07 1B
0B
3B 22 00 00 10 06 0A 06 06 06 06 06 06 06 0A 0A 06 06 06 0A 0A BB 1C 78 56
BC

In this sample the hex pair 48 represents the position in the line. It
lays
out at position 155/156 from the left. I need to change this to BLUE/BOLD
for
every instance of the ARES_INIDICATION and ARES_EINDICATION as the form
loads.


Hope that clears it up.

Thanks,


Nicholas Paldino said:
Brian,

What are you displaying this in? All you have shown is some content,
but not the mechanism by which you want to display it.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian Cook said:
I want to change the font color and weight at a specific position in the
text.

I am not sure where or what I need to do. It will be at position
155/156
from the left on each line.

Here is the code I need to put it into.

using System;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
bool firstLine = true;
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
int txPos;
int rxPos = -1;
if (firstLine)
firstLine = false;
else
sb.Append("\r\n");
if (((txPos = line.IndexOf("TX")) > -1) || ((rxPos =
line.IndexOf("RX")) > 0))
{
int charactersTillPoint;
if (txPos > -1)
charactersTillPoint = txPos;
else
charactersTillPoint = rxPos;
string part0 = line.Substring(0,
charactersTillPoint);
string part1 =
line.Substring(charactersTillPoint);
sb.Append(part0.PadRight(86));
sb.Append(part1);
}
else
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}

Thanks,
 
G

Guest

Thanks Nicholas, I will give that a try and let you know.

Brian

Nicholas Paldino said:
Brian,

What you will want to do in this case is for the sections that you want
to change the font/color of, you would select them using the
SelectedText/SelectedRtf properties, or the SelectionLength/SelectionStart
properties. Once you have the text selected that you want to format, you
can set the SelectionColor and SelectionFont properties to change the
font/color of the selected text.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian Cook said:
I have a text file that I modify from a compressed hex format into a single
line. This is the output of that part of the program that displays in a
RichTextBox;

2007/07/27 11:59:37 [123007]ARES_EINDICATION 010.050.016.004 407.3.01
(65e1) RX 68 bytes 65 E1 26 02 A4 5C AA 20 76 79 51 23 50 76 37
62
99 00 48 02 02 C7 88 01 C7 88 AA 50 76 37 62 99 20 76 79 51 23 D7 07 07 1B
0B
3B 22 00 00 10 06 0A 06 06 06 06 06 06 06 0A 0A 06 06 06 0A 0A BB 1C 78 56
BC

In this sample the hex pair 48 represents the position in the line. It
lays
out at position 155/156 from the left. I need to change this to BLUE/BOLD
for
every instance of the ARES_INIDICATION and ARES_EINDICATION as the form
loads.


Hope that clears it up.

Thanks,


Nicholas Paldino said:
Brian,

What are you displaying this in? All you have shown is some content,
but not the mechanism by which you want to display it.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I want to change the font color and weight at a specific position in the
text.

I am not sure where or what I need to do. It will be at position
155/156
from the left on each line.

Here is the code I need to put it into.

using System;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
bool firstLine = true;
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
int txPos;
int rxPos = -1;
if (firstLine)
firstLine = false;
else
sb.Append("\r\n");
if (((txPos = line.IndexOf("TX")) > -1) || ((rxPos =
line.IndexOf("RX")) > 0))
{
int charactersTillPoint;
if (txPos > -1)
charactersTillPoint = txPos;
else
charactersTillPoint = rxPos;
string part0 = line.Substring(0,
charactersTillPoint);
string part1 =
line.Substring(charactersTillPoint);
sb.Append(part0.PadRight(86));
sb.Append(part1);
}
else
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}

Thanks,
 
G

Guest

Nicholas, this is the routine I wrote, that DOES highlight the specific info
that I want highlighted.

The problem is it is time consuming(4mb file, 1-2 minutes) to load as it
populate the RichTextBox and highlights as it goes.

Is there a way I can have it do the same info before it popultes to the form?

Goal would be Open source log, read the log, and highlight the desired
information, then Show in the RichTextBox of the form.


---------------------------------------------------
private void findSequenceNumbers()
{
toolStripStatusLabel.Visible = toolStripProgressBar.Visible =
true;
toolStripProgressBar.Value = 0;
int lineNum = 0;
bool startingNewLine = true;
FontStyle style = FontStyle.Bold;
string[] lines = rtbTextBox.Lines;
string text = rtbTextBox.Text;
toolStripProgressBar.Maximum = text.Length;
for (int i = 0; i < text.Length; i++)
{
if (startingNewLine)
{
if ((lines[lineNum].Contains("ARES_EINDICATION")) ||
(lines[lineNum].Contains("ARES_INDICATION")))
{
i += 169;
rtbTextBox.Select(i, 2);
rtbTextBox.SelectionFont = new
Font(rtbTextBox.SelectionFont, rtbTextBox.SelectionFont.Style ^ style);
rtbTextBox.SelectionColor = Color.DarkBlue;
}
else if
(lines[lineNum].Contains("]CODELINE_INDICATION_MSG"))
{
i += 160;
rtbTextBox.Select(i, 2);
rtbTextBox.SelectionFont = new
Font(rtbTextBox.SelectionFont, rtbTextBox.SelectionFont.Style ^ style);
rtbTextBox.SelectionColor = Color.DarkBlue;
}
else
{
i += lines[lineNum].Length - 1;
}
startingNewLine = false;
Application.DoEvents();
}
if (text == '\n')
{
startingNewLine = true;
lineNum++;
}
toolStripProgressBar.Value = i;
}
toolStripStatusLabel.Visible = toolStripProgressBar.Visible =
false;
}
-------------------------------------------------------------

Thanks,



Nicholas Paldino said:
Brian,

What you will want to do in this case is for the sections that you want
to change the font/color of, you would select them using the
SelectedText/SelectedRtf properties, or the SelectionLength/SelectionStart
properties. Once you have the text selected that you want to format, you
can set the SelectionColor and SelectionFont properties to change the
font/color of the selected text.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian Cook said:
I have a text file that I modify from a compressed hex format into a single
line. This is the output of that part of the program that displays in a
RichTextBox;

2007/07/27 11:59:37 [123007]ARES_EINDICATION 010.050.016.004 407.3.01
(65e1) RX 68 bytes 65 E1 26 02 A4 5C AA 20 76 79 51 23 50 76 37
62
99 00 48 02 02 C7 88 01 C7 88 AA 50 76 37 62 99 20 76 79 51 23 D7 07 07 1B
0B
3B 22 00 00 10 06 0A 06 06 06 06 06 06 06 0A 0A 06 06 06 0A 0A BB 1C 78 56
BC

In this sample the hex pair 48 represents the position in the line. It
lays
out at position 155/156 from the left. I need to change this to BLUE/BOLD
for
every instance of the ARES_INIDICATION and ARES_EINDICATION as the form
loads.


Hope that clears it up.

Thanks,


Nicholas Paldino said:
Brian,

What are you displaying this in? All you have shown is some content,
but not the mechanism by which you want to display it.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I want to change the font color and weight at a specific position in the
text.

I am not sure where or what I need to do. It will be at position
155/156
from the left on each line.

Here is the code I need to put it into.

using System;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DatReader
{
class Streamer
{
public static string LayoutInput(string input)
{
StreamReader sr = File.OpenText(input);
StringBuilder sb = new StringBuilder(input.Length);
bool firstLine = true;
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Length < 29) { throw new
InvalidOperationException("invalid input"); }
if (line[29] != ' ')
{
int txPos;
int rxPos = -1;
if (firstLine)
firstLine = false;
else
sb.Append("\r\n");
if (((txPos = line.IndexOf("TX")) > -1) || ((rxPos =
line.IndexOf("RX")) > 0))
{
int charactersTillPoint;
if (txPos > -1)
charactersTillPoint = txPos;
else
charactersTillPoint = rxPos;
string part0 = line.Substring(0,
charactersTillPoint);
string part1 =
line.Substring(charactersTillPoint);
sb.Append(part0.PadRight(86));
sb.Append(part1);
}
else
sb.Append(line);
}
else
{
sb.Append(line.Substring(30));
}
}
return sb.ToString();
}
}
}

Thanks,
 
R

Roger Drost

I had the same requirement. i worked for days trying to come up with a solution. Finally i solved the problem by copying the RTB and pasting the copy right over the original making an exact duplicate on the form.

Then i alternated which RTB to use ... made it invisible, updated the data, then made it visible.

That made it lightning fast as it eliminated the annoying "screen scrolling" as each line was updated.

Then i made it visible ... problem solved.

HOWEVER .. there are some consequences of this approach ... for example, you have to keep track of which RTB the User clicked on ... etc etc etc.

I used one global variable to keep track of which RTB was most recently updated, RTBToUse.

the code looks like this:

***** prep:
// I am using 2 separate RTB controls so i can update out-of-sight
// to avoid the very agravating scrolling when "select" is used to update
// the colors of the reminder lines (on a massive scale)

if (RTBToUse == "RemRTB1") // swap them each time thru
{
RTBToUse = "RemRTB2";
RemRTB2.Visible = false; // Hide it while we update
RemRTB2.Clear(); // Clear it
}
else
{
RTBToUse = "RemRTB1";
RemRTB1.Visible = false; // Hide it while we update
RemRTB1.Clear();
}


*** then update your data ***

if (RTBToUse == "RemRTB1")
{
RemRTB1.SelectionColor = remColor[n];
RemRTB1.AppendText(aLine + System.Environment.NewLine);
}
else
{
RemRTB2.SelectionColor = remColor[n];
RemRTB2.AppendText(aLine + System.Environment.NewLine);
}

*** finish wih:

if (RTBToUse == "RemRTB1")
{
RemRTB1.Select(0, 0);
RemRTB1.Visible = true;
}
else
{
RemRTB2.Select(0, 0);
RemRTB2.Visible = true;
}


EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
Top