Get font and color from RichTextBox

  • Thread starter Thread starter Palli
  • Start date Start date
P

Palli

Hi

Im trying to create image from RichTextBox text but I run
into small problem? The problem is when I try to select
current font or color from the text in the rtb. The
returned font and color are always the font and color at
the begin of the text. This is my code

for (int iCurChar = lastChar; iCurChar <
richTextBox1.Text.Length; iCurChar++)
{
richTextBox1.Select(iCurChar,1);
char CurrChar = richTextBox1.Text[iCurChar];
Color CurrColor = richTextBox1.SelectionColor;
Font CurrFont = richTextBox1.SelectionFont;
int iFontHeight = CurrFont.Height;

....

Does anybody know what my problem is?

Thanks
Palli
 
Hi

Im trying to create image from RichTextBox text but I run
into small problem? The problem is when I try to select
current font or color from the text in the rtb. The
returned font and color are always the font and color at
the begin of the text. This is my code

for (int iCurChar = lastChar; iCurChar <
richTextBox1.Text.Length; iCurChar++)
{
richTextBox1.Select(iCurChar,1);
char CurrChar = richTextBox1.Text[iCurChar];
Color CurrColor = richTextBox1.SelectionColor;
Font CurrFont = richTextBox1.SelectionFont;
int iFontHeight = CurrFont.Height;

...

Does anybody know what my problem is?

Thanks
Palli

Palli

SelectionColor and SelectionFont return the Colour and Font of
the currently selected text.

The help file says:

If the current text selection has more than one color specified,
this property returns Color.Empty. If no text is currently
selected, the text color specified in this property is applied
to the current insertion point and to all text that is typed
into the control after the insertion point. The text color
setting applies until the property is changed to a different
color or until the insertion point is moved to a different
section within the control.

What do you have selected when you call your function?
 
Hi Jeff

Im not sure what you meen but I read each character and
try to get current font and color on selected point.

richTextBox1.Select(iCurChar,1);

This sets the point to current position? Right??

Then I call these functions

Color CurrColor = richTextBox1.SelectionColor;
Font CurrFont = richTextBox1.SelectionFont;

I thought that when I have set the Select(iCurChar,1) then
I could call selectioncolor and selectionfont and get the
color and font of selected text. But know it always
return the text and color from the first position.

Hope this gives you idea what Im thinking and trying to do.

Regards
Palli

-----Original Message-----
Hi

Im trying to create image from RichTextBox text but I run
into small problem? The problem is when I try to select
current font or color from the text in the rtb. The
returned font and color are always the font and color at
the begin of the text. This is my code

for (int iCurChar = lastChar; iCurChar <
richTextBox1.Text.Length; iCurChar++)
{
richTextBox1.Select(iCurChar,1);
char CurrChar = richTextBox1.Text[iCurChar];
Color CurrColor = richTextBox1.SelectionColor;
Font CurrFont = richTextBox1.SelectionFont;
int iFontHeight = CurrFont.Height;

...

Does anybody know what my problem is?

Thanks
Palli

Palli

SelectionColor and SelectionFont return the Colour and Font of
the currently selected text.

The help file says:

If the current text selection has more than one color specified,
this property returns Color.Empty. If no text is currently
selected, the text color specified in this property is applied
to the current insertion point and to all text that is typed
into the control after the insertion point. The text color
setting applies until the property is changed to a different
color or until the insertion point is moved to a different
section within the control.

What do you have selected when you call your function?
 
Hi Jeff

Im not sure what you meen but I read each character and
try to get current font and color on selected point.

richTextBox1.Select(iCurChar,1);

This sets the point to current position? Right??

Then I call these functions

Color CurrColor = richTextBox1.SelectionColor;
Font CurrFont = richTextBox1.SelectionFont;

I thought that when I have set the Select(iCurChar,1) then
I could call selectioncolor and selectionfont and get the
color and font of selected text. But know it always
return the text and color from the first position.

Hope this gives you idea what Im thinking and trying to do.

Regards
Palli

Hi Palli

I set up a test with a RichTextBox containing:

'Red Green Blue'

with each word in the indicated colour then pasted your code in
and logged the following output:

JWriteLog(CurrChar + " " + CurrColor.ToString() +
CurrFont.ToString(), false);

R Color [Red][Font: Name=Times New Roman, Size=12, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
e Color [Red][Font: Name=Times New Roman, Size=12, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
d Color [Red][Font: Name=Times New Roman, Size=12, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
Color [Red][Font: Name=Times New Roman, Size=12, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
G Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
r Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
e Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
e Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
n Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
B Color [Blue][Font: Name=Times New Roman, Size=16, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
l Color [Blue][Font: Name=Times New Roman, Size=16, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
u Color [Blue][Font: Name=Times New Roman, Size=16, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
e Color [Blue][Font: Name=Times New Roman, Size=16, Units=3,
GdiCharSet=0, GdiVerticalFont=False]

Is that what you expected?

If you're not getting that result is it because the RichTextBox
perhaps contains only normal black text,i.e. not coloured RTF
Text?

It seems to show that your code works OK.
 
Hi Jeff

Its good to know that my code is right, but the text in my
rtb is colored! Is maybe some properties or something
that aren't right. I'll take a look at that.

Thanks
Palli

-----Original Message-----
Hi Jeff

Im not sure what you meen but I read each character and
try to get current font and color on selected point.

richTextBox1.Select(iCurChar,1);

This sets the point to current position? Right??

Then I call these functions

Color CurrColor = richTextBox1.SelectionColor;
Font CurrFont = richTextBox1.SelectionFont;

I thought that when I have set the Select(iCurChar,1) then
I could call selectioncolor and selectionfont and get the
color and font of selected text. But know it always
return the text and color from the first position.

Hope this gives you idea what Im thinking and trying to do.

Regards
Palli

Hi Palli

I set up a test with a RichTextBox containing:

'Red Green Blue'

with each word in the indicated colour then pasted your code in
and logged the following output:

JWriteLog(CurrChar + " " + CurrColor.ToString() +
CurrFont.ToString(), false);

R Color [Red][Font: Name=Times New Roman, Size=12, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
e Color [Red][Font: Name=Times New Roman, Size=12, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
d Color [Red][Font: Name=Times New Roman, Size=12, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
Color [Red][Font: Name=Times New Roman, Size=12, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
G Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
r Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
e Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
e Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
n Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
B Color [Blue][Font: Name=Times New Roman, Size=16, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
l Color [Blue][Font: Name=Times New Roman, Size=16, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
u Color [Blue][Font: Name=Times New Roman, Size=16, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
e Color [Blue][Font: Name=Times New Roman, Size=16, Units=3,
GdiCharSet=0, GdiVerticalFont=False]

Is that what you expected?

If you're not getting that result is it because the RichTextBox
perhaps contains only normal black text,i.e. not coloured RTF
Text?

It seems to show that your code works OK.
 
Hi

I found out what corrupt my RTB. I added a '\n' as a last
character in my rtb and it f****d the text up. My code was
like that;

rtb.text += '\n';

and this line transformed all the rtf in my box.

Thanks Jeff for all the help.

Palli
-----Original Message-----
Hi Jeff

Its good to know that my code is right, but the text in my
rtb is colored! Is maybe some properties or something
that aren't right. I'll take a look at that.

Thanks
Palli

-----Original Message-----
Hi Jeff

Im not sure what you meen but I read each character and
try to get current font and color on selected point.

richTextBox1.Select(iCurChar,1);

This sets the point to current position? Right??

Then I call these functions

Color CurrColor = richTextBox1.SelectionColor;
Font CurrFont = richTextBox1.SelectionFont;

I thought that when I have set the Select(iCurChar,1) then
I could call selectioncolor and selectionfont and get the
color and font of selected text. But know it always
return the text and color from the first position.

Hope this gives you idea what Im thinking and trying to do.

Regards
Palli

Hi Palli

I set up a test with a RichTextBox containing:

'Red Green Blue'

with each word in the indicated colour then pasted your code in
and logged the following output:

JWriteLog(CurrChar + " " + CurrColor.ToString() +
CurrFont.ToString(), false);

R Color [Red][Font: Name=Times New Roman, Size=12, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
e Color [Red][Font: Name=Times New Roman, Size=12, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
d Color [Red][Font: Name=Times New Roman, Size=12, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
Color [Red][Font: Name=Times New Roman, Size=12, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
G Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
r Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
e Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
e Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
n Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
Color [Lime][Font: Name=Times New Roman, Size=14, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
B Color [Blue][Font: Name=Times New Roman, Size=16, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
l Color [Blue][Font: Name=Times New Roman, Size=16, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
u Color [Blue][Font: Name=Times New Roman, Size=16, Units=3,
GdiCharSet=0, GdiVerticalFont=False]
e Color [Blue][Font: Name=Times New Roman, Size=16, Units=3,
GdiCharSet=0, GdiVerticalFont=False]

Is that what you expected?

If you're not getting that result is it because the RichTextBox
perhaps contains only normal black text,i.e. not
coloured
RTF
Text?

It seems to show that your code works OK.
.
 
Back
Top