473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

RichTextBox.selection.color

DG
i do:

for(int i=0; i<10; i++)
{
txtLog.Text = i.ToString() + "\r\n" + txtLog.Text;
startSelection = txtLog.SelectionStart;
lenghtSelection = txtLog.SelectionLength;
txtLog.Select(0, i.ToString().Length);

if(i%2 == 0)
{
txtLog.SelectionColor = Color.Black;
MessageBox.Show("");
}
else
{
txtLog.SelectionColor = Color.Red;
MessageBox.Show("");
}
txtLog.Select(startSelection, lenghtSelection);

}

i expect every other row to be different color but
first row get colored well, but afer new row is colored in new color, all
rows under are colored to old color

e.g:

(5th step)
black
red
red
red
red

(6th step)
red
black
black
black
black
black

when does coloring actually occur? after SelectionColor?
Nov 17 '05 #1
4 6197
DG <dg@zg.htnet.hr> wrote:
for(int i=0; i<10; i++)
{
txtLog.Text = i.ToString() + "\r\n" + txtLog.Text;
Here, the text is written using the colour that was last set. When you set
the colour below, it continues to use that colour here.
startSelection = txtLog.SelectionStart;
lenghtSelection = txtLog.SelectionLength;
Since you just replaced all the text, the selection was just lost.
Therefore,

txtLog.SelectionStart == txtLog.SelectionLength == 0

at this point.

<snip>
txtLog.Select(startSelection, lenghtSelection);


This is equivalent to

txtLog.Select(0, 0);

(see above). However, this is also pointless for another reason: at the
top of the loop, you replace the text, whereupon these values are reset.

<snip>

Here's a working solution:

{
txtLog.Clear();
for(int i=0; i<10; i++)
{
txtLog.Select(0, 0);
txtLog.SelectionColor = i%2 == 0 ? Color.Black : Color.Red;
txtLog.SelectedText = i.ToString() + "\r\n";
}
}
Nov 17 '05 #2
DG
i don't quite get it, how does SelectedText property work?
it inserts new text in RichTexbox?

"C# Learner" <cs****@learner.here> wrote in message
news:1w****************@csharp.learner...
DG <dg@zg.htnet.hr> wrote:
for(int i=0; i<10; i++)
{
txtLog.Text = i.ToString() + "\r\n" + txtLog.Text;


Here, the text is written using the colour that was last set. When you
set
the colour below, it continues to use that colour here.
startSelection = txtLog.SelectionStart;
lenghtSelection = txtLog.SelectionLength;


Since you just replaced all the text, the selection was just lost.
Therefore,

txtLog.SelectionStart == txtLog.SelectionLength == 0

at this point.

<snip>
txtLog.Select(startSelection, lenghtSelection);


This is equivalent to

txtLog.Select(0, 0);

(see above). However, this is also pointless for another reason: at the
top of the loop, you replace the text, whereupon these values are reset.

<snip>

Here's a working solution:

{
txtLog.Clear();
for(int i=0; i<10; i++)
{
txtLog.Select(0, 0);
txtLog.SelectionColor = i%2 == 0 ? Color.Black : Color.Red;
txtLog.SelectedText = i.ToString() + "\r\n";
}
}

Nov 17 '05 #3
DG <dg@zg.htnet.hr> wrote:
i don't quite get it, how does SelectedText property work?
it inserts new text in RichTexbox?


If SelectionLength is 0, then it inserts the text at position
SelectionStart. If SelectionLength is greater than 0, then it replaces the
current selection.
Nov 17 '05 #4
DG
Thank you!
"C# Learner" <cs****@learner.here> wrote in message
news:5m**************@csharp.learner...
DG <dg@zg.htnet.hr> wrote:
i don't quite get it, how does SelectedText property work?
it inserts new text in RichTexbox?


If SelectionLength is 0, then it inserts the text at position
SelectionStart. If SelectionLength is greater than 0, then it replaces
the
current selection.

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael A. Covington | last post by:
When you change the Font property of a RichTextBox, does it change the font of all the contents, or only the text subsequently added, or only the selection, or what?
2
by: Hariharan S | last post by:
Hi Guys, I have a string, say "Hello World" and would like to format the way I want to (say, wanna have the Hello to be bold italic and the World to be of font size 14 or so) and put it in the...
1
by: John Baro | last post by:
1) When you select the "absolute" end of a richtextbox (go to last character, hold down "shift" and click right keyboard button), the font changes to the default of the richtextbox. How can I set...
2
by: Ludovic SOEUR | last post by:
Suppose I have a .Net C# Winforms App with a RichTextBox control and I want to provide an option for the user to open the Rtf in Word, edit it in Word and get the edited value back to my C# app...
0
by: Brian Scott | last post by:
I am currently trying to control the syntax highlighting of a richtextbox control whilst the user types. At the moment I have implemented a thread which calls the richTextBox.Invoke on a method...
2
by: Clark Stevens | last post by:
Hi. This should be so easy, but I don't get it. Let say I have RichTextbox1 and I want to insert some text at the current insertion point, or at the beginning of selected text (if there is any). ...
3
by: sss024 | last post by:
Word document viewing in RichTextBox control
1
by: Leon Friesema | last post by:
Hi, I'm working on a RichTextBox with some "in-program" style-features (e.g. *no way* becomes bold : like MS Word does). Got everything working fine, flickerfree and reasonable fast, but in...
2
by: =?Utf-8?B?TWFyayBDb2xsYXJk?= | last post by:
I'm currently writing an application that has a RichTextBox and a toolbar which includes the following toolbar buttons: - Font - Font Size - Bold - Italic - Underline I want the toolbox...
0
by: TAB | last post by:
"none" <none@none.comskrev i meddelandet news:eaJIxCA7IHA.2260@TK2MSFTNGP03.phx.gbl... Use richtextbox selection properties. Like richTextBox.SelectedText or SelectedRtf if you would like to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.