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? 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";
}
}
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"; } }
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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?
|
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...
|
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...
|
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...
|
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...
|
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). ...
|
by: sss024 |
last post by:
Word document viewing in RichTextBox control
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: 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: 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...
|
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,...
| |