473,796 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RichTextBox.sel ection.color

DG
i do:

for(int i=0; i<10; i++)
{
txtLog.Text = i.ToString() + "\r\n" + txtLog.Text;
startSelection = txtLog.Selectio nStart;
lenghtSelection = txtLog.Selectio nLength;
txtLog.Select(0 , i.ToString().Le ngth);

if(i%2 == 0)
{
txtLog.Selectio nColor = Color.Black;
MessageBox.Show ("");
}
else
{
txtLog.Selectio nColor = Color.Red;
MessageBox.Show ("");
}
txtLog.Select(s tartSelection, 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 6231
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.Selectio nStart;
lenghtSelection = txtLog.Selectio nLength;
Since you just replaced all the text, the selection was just lost.
Therefore,

txtLog.Selectio nStart == txtLog.Selectio nLength == 0

at this point.

<snip>
txtLog.Select(s tartSelection, 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.Selectio nColor = i%2 == 0 ? Color.Black : Color.Red;
txtLog.Selected Text = 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.Selectio nStart;
lenghtSelection = txtLog.Selectio nLength;


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

txtLog.Selectio nStart == txtLog.Selectio nLength == 0

at this point.

<snip>
txtLog.Select(s tartSelection, 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.Selectio nColor = i%2 == 0 ? Color.Black : Color.Red;
txtLog.Selected Text = 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.l earner...
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
3959
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
17290
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 RichTextBox, I created. Just want to paste or write this string to the RichTextBox; what command should I use? I used the following command and it did not help ... richTextBox.SelectionFont = <some font selected>
1
445
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 this so that when the user changes font, if its the end of the rtb, this font changes as well? 2)Why is it that when multiple fonts are selected, the selectionfont property does not return null? 3)Same with the size.
2
6029
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 dynamically (not with an external file). Where should I start? Is there a document explaining this? I read msdn but I did not see exactly what I am looking for thanks,
0
1667
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 that parses each line of the richTextBox and highlights specific words with a given color. I have been highlighting each of the words by using the Select method and changing the selection color beforehand. This works fine until the contents of the...
2
6072
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). How is this done? Also, how do I delete the current selection? Thanks.
3
8726
by: sss024 | last post by:
Word document viewing in RichTextBox control
1
3063
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 order to set the style of some part of the text I use the "Selection" method (in the case of *text* the richTextBox1.SelectionFont.Bold = true
2
3548
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 buttons to act the same as the ones in Word. i.e. When you select text that has different Fonts the Font toolbar button (combo box)
0
1921
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 keep the rtf formatting, SelectionColor, SelectionBackColor, SelectionFont and so on. e.g. string str = richtextBox1.SelectedText;
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.