-
public void logger_reader(string logger_reader)
-
{
-
string item = @"Last";
-
//*** Coloring all the text inside the richTextBox1 in Red ***\\
-
string[] ftext = File.ReadAllLines(logger_reader);
-
for (int i = 0; i < ftext.Length; i++)
-
{
-
richTextBox1.SelectionColor = Color.Red;
-
richTextBox1.GetFirstCharIndexFromLine(i);
-
richTextBox1.SelectedText=(ftext[i] + "\r\n");
-
}
-
So this code will color in Red all the text inside the richTextBox1. But now i want to color in Red all the lines that the word "Last" ( string item ) appears and to color all the lines from start to end that the word is appears there. Not to color in Red only the word it self "Last" but color in Red the whole line containing the word "Last" and all the other lines that wont contain this word will stay in black. And please use my code here.
Thank you very much for the help.
15 2285
I think you only missed an if-statement within the for-loop.
e.g. -
for (int i = 0; i < ftext.Length; i++)
-
{
-
//check if line contains the word last (string item)
-
if(ftext[i].Contains(item)) {
-
//do the coloring as above
-
}
-
}
-
Christian Binder i did what you said but still its painting all the text inside the richTextBox1 in Red.
This is what i changed now: -
public void logger_reader(string logger_reader)
-
{
-
string item = @"Last";
-
//*** Coloring all the text inside the richTextBox1 in Red ***\\
-
string[] ftext = File.ReadAllLines(logger_reader);
-
-
for (int i = 0; i < ftext.Length; i++)
-
{
-
if (ftext[i].Contains(item))
-
{
-
richTextBox1.SelectionColor = Color.Red;
-
richTextBox1.GetFirstCharIndexFromLine(i);
-
richTextBox1.SelectedText = (ftext[i] + "\r\n");
-
}
-
}
-
I tried a few things and figured out, that first the selection has to be done and after the selection you've to set the SelectionColor to color the selected text.
Also I didn't have used SelectedText but SelectionStart and SelectionLength, maybe it works also with SelectedText. -
for (int i = 0; i < ftext.Length; i++) {
-
int start = richTextBox1.GetFirstCharIndexFromLine(i);
-
richTextBox1.SelectionStart = start;
-
richTextBox1.SelectionLength = ftext[i].Length;
-
-
//When keyword is found, color selection red otherwise black
-
if (ftext[i].Contains(item))
-
richTextBox1.SelectionColor = Color.Red;
-
else
-
richTextBox1.SelectionColor = Color.Black;
-
}
-
Christian Binder i did it as you did but im getting now error exception on the line: -
richTextBox1.SelectionStart = start;
-
The error say : InvalidArgument=Value of '-1' is not valid for 'SelectionStart'.
Parameter name: SelectionStart
And this is the code i did same as oyurs so i dont understand why im getting this exception:
The code it self : -
public void logger_reader(string logger_reader)
-
{
-
string item = @"Last";
-
//*** Coloring all the text inside the richTextBox1 in Red ***\\
-
string[] ftext = File.ReadAllLines(logger_reader);
-
-
for (int i = 0; i < ftext.Length; i++)
-
{
-
int start = richTextBox1.GetFirstCharIndexFromLine(i);
-
richTextBox1.SelectionStart = start;
-
richTextBox1.SelectionLength = ftext[i].Length;
-
-
if (ftext[i].Contains(item))
-
richTextBox1.SelectionColor = Color.Red;
-
else
-
richTextBox1.SelectionColor = Color.Black;
-
-
}
-
Maybe cuz my text file wich is logger_reader is big like 127kb?
Anyway i dont know how to fix this exception.
The full exception if its important : -
The full exception:
-
-
System.ArgumentOutOfRangeException was unhandled
-
Message=InvalidArgument=Value of '-1' is not valid for 'SelectionStart'.
-
Parameter name: SelectionStart
-
Source=System.Windows.Forms
-
ParamName=SelectionStart
-
StackTrace:
-
at System.Windows.Forms.TextBoxBase.set_SelectionStart(Int32 value)
-
at WindowsFormsApplication1.Logger_Text_Reader.logger_reader(String logger_reader) in D:\C-Sharp\Download File\Downloading-File-Project-Version-011\Downloading File\Downloading File\Logger_Text_Reader.cs:line 36
-
at WindowsFormsApplication1.Form1.View_Log_File_Click(Object sender, EventArgs e) in D:\C-Sharp\Download File\Downloading-File-Project-Version-011\Downloading File\Downloading File\Form1.cs:line 946
-
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
-
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
-
at System.Windows.Forms.Control.WndProc(Message& m)
-
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
-
at System.Windows.Forms.Button.WndProc(Message& m)
-
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
-
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
-
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
-
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
-
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
-
at WindowsFormsApplication1.Program.Main() in D:\C-Sharp\Download File\Downloading-File-Project-Version-011\Downloading File\Downloading File\Program.cs:line 27
-
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
-
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
-
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
-
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
-
at System.Threading.ThreadHelper.ThreadStart()
-
InnerException:
-
Do you fill the richTextBox?
I filled it up before by using richTextBox1.Text = File.ReadAllText(@"C:\xxx.txt");
Christian i do fill the richtextobx1 liek that:
string[] ftext = File.ReadAllLines(logger_reader);
But ill change it to how you did it.
I just wonder then in your code above where from did you take the ftext i used?
Cuz when your filling the richtextbox1 there is no ftext.
Thanks.
ftext is just a variable, which has nothing to do with my RichTextBox.
I just used it for having the lines.
So at least, I load the file twice, the first time into the RichTextBox, the second time into the ftext-array.
Christian This is the code now i change the filling of the richTextBox1 as you did.
But what do i do with the ftext im getting error on all the places since its not exist with what should i replace it now? -
public void logger_reader(string logger_reader)
-
{
-
string item = @"Last";
-
//*** Coloring all the text inside the richTextBox1 in Red ***\\
-
// string[] ftext = File.ReadAllLines(logger_reader);
-
richTextBox1.Text = File.ReadAllText(logger_reader);
-
for (int i = 0; i < ftext.Length; i++)
-
{
-
int start = richTextBox1.GetFirstCharIndexFromLine(i);
-
richTextBox1.SelectionStart = start;
-
richTextBox1.SelectionLength = ftext[i].Length;
-
-
if (ftext[i].Contains(item))
-
richTextBox1.SelectionColor = Color.Red;
-
else
-
richTextBox1.SelectionColor = Color.Black;
-
-
}
-
The logger_reader getting from Form1 logger.txt wich is 127kb text file so the richTextBox1 is now filled same wya you did just what to do with the ftext rplace it with what?
Thanks.
You've to use both lines. So uncomment the commented-out line. -
//*** Coloring all the text inside the richTextBox1 in Red ***\\
-
string[] ftext = File.ReadAllLines(logger_reader);
-
richTextBox1.Text = File.ReadAllText(logger_reader);
-
for (int i = 0; i < ftext.Length; i++)
-
...
-
Christian i used now with the two fillings lines the array ftext and the second one and what it does when i open the log file ( richTextBox1 ) i see its starting from the middle of the file and all the above part is colored with Red where there is "Last" but also many lines with "Last" are black.
And all the other text from this point the middle and bottom to the end of the text is colored with black even there are lines with "Last" in it.
That's strange, I tried it with your code above and it worked correctly.
Maybe it's because of the file-size?
Try a test-file with just a few contents (e.g. 10 lines) and look if it works?
Did you set any parameters of richTextBox1 in the Designer?
Christinan wich parameters should i set in the deisginer about the richTextBox1? And set them to what?
And i tested now the same file but with few lines the file size is 1kb and its working!
But my original file is 174kb and then its not working good.
Any ideas how to solve the size problem?
Thanks.
I don't know :-(
Can you post your file here, so I can try it?
I found out, that if the RichTextBox is big enough that no line is wrapped,
it works great.
Only if the richtextbox is smaller than some lines, wrong text gets colored ...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Thorsten moeller |
last post by:
Hi,
i have a Textbox and amethod adding lines to the Textbox. There are two kind lines, depending on a check to be valid or invalid.
Is it possible to color the lines or the text in a textbox...
|
by: Moondog |
last post by:
My string looks something like this:
strCaption = "Item Number " & strItemNumber
I would like to present the strItemNumber portion in a different color
than the rest of the string so it...
|
by: Gidi |
last post by:
Hi,
Is it possible to Disable editing of one specific cell in one specific row
in dataGrid?
Thanks,
Gidi.
|
by: Yi Xing |
last post by:
Hi All,
I want to read specific lines of a huge txt file (I know the line #).
Each line might have different sizes. Is there a convenient and fast
way of doing this in Python? Thanks.
Yi Xing
|
by: foolproofplan |
last post by:
I have a somewhat simple task I need to do, but since I am new at xml,
I need help:
Right now, I have xml files that are output from tests I do with an
automated testing program. I want to...
|
by: blazedaces |
last post by:
Alright guys, so the title explains exactly my goal. The truth is I'm going to be reading in a lot of data from an xml file. The file is too large and there's too much data to store in arraylists...
|
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= |
last post by:
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...
|
by: Francesco Pietra |
last post by:
Please, how to adapt the following script (to delete blank lines) to delete
lines containing a specific word, or words?
f=open("output.pdb", "r")
for line in f:
line=line.rstrip()
if line:...
|
by: Francesco Pietra |
last post by:
I forgot to add that the lines to strip are in present case of the type of the
following block
HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O
HETATM 7008 H1 WAT...
|
by: masha2011 |
last post by:
Hello all, I am very new to programming and I have recently started using python. I am interested in creating a file to essentially compare data points.
I am curious if it is possible to read...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |