Lukas831

Hi there!

Is it possible to change colors of some words in RichTextBox without selecting them. I mean : I want to change color of word which starts at index 1 and end at 5. Off course I can select it, do SelectedFont() method and unselect - but is it any way to do that without select

I don`t know how this method work (method to change colors), but there must be some way to do that!

Second question: how to disable auto-scrolling in RichTextBox


Re: Visual C# Express Edition RichTextBox - change font of words without selecting?

paoloTheCool

Hmmm, well I do not have .Net 3.0 yet but you may want to take a look at this article: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1191437&SiteID=1

The code from that article:

Code Block

RichTextBox rtb = new RichTextBox();

TextRange tr = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);

tr.Select(rtb.Document.Blocks.FirstBlock.ContentStart, rtb.Document.Blocks.FirstBlock.ContentEnd);

It looks like you can get a text range, and edit based on that range. I can't figure out how to do this in .Net 2.0. There is not a TextRange class or RichTextBox.Document class. Maybe in 3.0, you can do something like tr.Forecolor = COLOR. Instead of tr.Select().

Thanks,

paoloTheCool





Re: Visual C# Express Edition RichTextBox - change font of words without selecting?

Suprotim Agarwal

To disable autoscrolling, in the Form_load event add,

rtb.ScrollBars = RichTextBoxScrollBars.None

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----






Re: Visual C# Express Edition RichTextBox - change font of words without selecting?

Cameron Kloot

well, the best way to select text in a richtextbox is to use the selectionfont method like you talked about in your first post. example:
richTextBox1.Select(1, 5);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.Select(1, 0);
richTextBox1.SelectionColor = Color.Black;


if i read your second question right, u want it so that when you add text to the richtext box, and the text is too long to fit normaly, the control doesnt scroll down to the bottom of the text when it is added. right if so, you should use the richTextBox1.ScrollToCaret(); method like so:

richTextBox1.SelectionStart = 5; // this is where you want the richtextbox to scroll, // so top of the text, just put "0"
richTextBox1.ScrollToCaret();
hope that helps





Re: Visual C# Express Edition RichTextBox - change font of words without selecting?

paoloTheCool

I guess the best way to do it if you do not have .Net 3.0 would be Cameron's method.

Except I do not know why you would need the four lines:

Code Block

richTextBox1.Select(1, 5);

richTextBox1.SelectionColor = System.Drawing.Color.Red;

richTextBox1.Select(0, 0);

should work.

Just out of curiousity, why would you want to not do it by selecting Is it that there is already text selected, and you want to keep it selected, because if that is the case, this method would not be very good.

Thanks,

paoloTheCool





Re: Visual C# Express Edition RichTextBox - change font of words without selecting?

Cameron Kloot

yes, it would work without the fourth line of code. if you dont want to unselect something you can just store the values in variables.
int i = richtextbox1.SelectionStart;
int ii = richtextbox1.SelectionText.Length;

and then after running the other code:
richtextbox1.Select(i, ii);





Re: Visual C# Express Edition RichTextBox - change font of words without selecting?

Lukas831

well, I know how to solve this problem by "select" .. just wondering is there any way to do this without selecting. I can`t use select because I write a Syntax highlight RichTextbox (there are serious problems with selecting).

Anyway - this TextRange class from .NET 3.0 should be fine .. thx





Re: Visual C# Express Edition RichTextBox - change font of words without selecting?

Lukas831

okej, downloaded i installed .NET 3.5 - there is no TextRange class Sad




Re: Visual C# Express Edition RichTextBox - change font of words without selecting?