Changing a richtextbox's colour!

  • Thread starter Thread starter Hugh Janus
  • Start date Start date
H

Hugh Janus

OK, this is driving me crazy! As far as I can tell it should work. It
compiles fine.

I am saving the fore and back colour of a RTB to the registry like
this:

Srx.SetValue("ForeColour", Console.ForeColor.ToArgb.ToString)
Srx.SetValue("BackColour", Console.BackColor.ToArgb.ToString)

Then, I try to read the colours back in at runtime and change the
colour of the RTB like this:

Dim ForeCol As New System.Drawing.Color
Dim BackCol As New System.Drawing.Color

ForeCol.FromName(Lrx.GetValue("ForeColour"))
Console.ForeColor = ForeCol
BackCol.FromName(Lrx.GetValue("BackColour"))
Console.BackColor = BackCol

The colours get saved to the registry OK but when the form loads it is
not changing the colours! As far as I can tell the values are read
back from the registry OK but just ignored.

Any ideas?? Thanks.
 
Hi Hugh,
Srx.SetValue("ForeColour", Console.ForeColor.ToArgb.ToString)
Srx.SetValue("BackColour", Console.BackColor.ToArgb.ToString)

It looks like your saving to an Argb value here...
Dim ForeCol As New System.Drawing.Color
Dim BackCol As New System.Drawing.Color

ForeCol.FromName(Lrx.GetValue("ForeColour"))
Console.ForeColor = ForeCol
BackCol.FromName(Lrx.GetValue("BackColour"))
Console.BackColor = BackCol

But loading from a Name here. Use FromArgb instead.

I hope this helps.

Nick.
 
But loading from a Name here. Use FromArgb instead.

I hope this helps.

Nick.

Thanks, I never saw that. I have changed it but it makes no
difference. The colours loaded are still black text on white
background.

Hugh
 
Hugh Janus said:
I am saving the fore and back colour of a RTB to the registry like
this:

Srx.SetValue("ForeColour", Console.ForeColor.ToArgb.ToString)
Srx.SetValue("BackColour", Console.BackColor.ToArgb.ToString)

Then, I try to read the colours back in at runtime and change the
colour of the RTB like this:

Dim ForeCol As New System.Drawing.Color
Dim BackCol As New System.Drawing.Color

ForeCol.FromName(Lrx.GetValue("ForeColour"))

\\\
Me.RichTextBox1.BackColor = _
Color.FromName(CInt(Lrx.GetValue("ForeColor")))
///
 
\\\
Me.RichTextBox1.BackColor = _
Color.FromName(CInt(Lrx.GetValue("ForeColor")))
///

Thanks, but still no joy. It is just ignored. Here is my modified
code.

Srx.SetValue("ForeColour", Console.ForeColor.Name)
Srx.SetValue("BackColour", Console.BackColor.Name)

Console.ForeColor =
System.Drawing.Color.FromName(CInt(Lrx.GetValue("ForeColor")))
Console.BackColor =
System.Drawing.Color.FromName(CInt(Lrx.GetValue("BackColor")))

The values in the registry are:
ForeColour - ffff8080
BackColour - ff408080


Hugh
 
Hugh Janus said:
Thanks, but still no joy. It is just ignored. Here is my modified
code.

Sorry for the typo, I actually wanted to type 'Color.FromArgb'! If you are
storing color names in the registry, use 'Color.FromName'. If you are
storing color values, use 'Color.FromArgb'.
 
Sorry for the typo, I actually wanted to type 'Color.FromArgb'! If you are
storing color names in the registry, use 'Color.FromName'. If you are
storing color values, use 'Color.FromArgb'.

Hmmm, now I am even more confused than ever. Is there not an easier
way? I simply want to store the colour as a string in the registry (be
it in the format ffff8080 or whatever) and then on load, read the value
and set the colour of the RTB!!

Lost! Please help!

Hugh
 
Hi Hugh,

I see no reason why using ToArgb and FromArgb shouldn't work. But one
thing you can try is the ColorConverter class,

ColorTranslater.ToWin32 and FromWin32 methods to obtain a color value
from a 32 bit integer.

Make sure you are loading them from the registry correctly first by
popping them up on a message box or displaying to the console before passing
to the rich text box.

Nick.
 
Hang on a second,

Try changing the name of your rich textbox to something other than
Console...
 
I see no reason why using ToArgb and FromArgb shouldn't work. But one
OK, this is my new code after lots of playing around.

Dim SColour As Color
SColour = Color.FromArgb(txtConsole.ForeColor.ToArgb)
Dim RColour As String = SColour.ToString

Srx.SetValue("ForeColour", RColour)

That writes the value "Color [A=255, R=255, G=255, B=128]" to the
registry for a sort of off-yellow colour.

....

Then, I read it back in with this:

Dim LColour As Color = Color.FromName(Lrx.GetValue("ForeColour"))
txtConsole.ForeColor = LColour


The code MsgBox(LColour.ToString & Chr(13) &
Lrx.GetValue("ForeColour")) returns:

"Color [Color [A=255, R=255, G=255, B=128]]
Color [A=255, R=255, G=255, B=128]"

I have no idea where the extra word 'color' came from nor the end ] but
the colour is not changed on my RTB.

Has nobody ever done anything similar before? I get no compile errors
not errors when it is supposed to change the colour.

Hugh
 
Also,

Console.WriteLineLColour.ToString & Chr(13) &
Lrx.GetValue("ForeColour") & Chr(13) & txtConsole.ForeColor.ToString)

returns this:

Color [Color [A=255, R=255, G=128, B=0]]
Color [A=255, R=255, G=128, B=0]
Color [Color [A=255, R=255, G=128, B=0]]

As you can see, it has the same colour as the colour object but on the
form it is displayed as black.
 
SOLVED.

Well, after sheer playing around and guessing (it tends to work when
experience doesn't!!!) I have solved it although I have no idea why
this works and it never before did. So here is the code:



Private ForeC As Drawing.Color
Private BackC As Drawing.Color

----------------------
'This is part of the sub that shows the colour picker and then sets the
colours.

Dim ColChooser As New ColorDialog

ColChooser.ShowDialog()
txtConsole.ForeColor = ColChooser.Color
ForeC = ColChooser.Color

ColChooser.ShowDialog()
txtConsole.BackColor = ColChooser.Color
BackC = ColChooser.Color

---------------------------------
'This is part of the sub that writes to the registry.

Srx.SetValue("ForeColour", ForeC.ToArgb.ToString) ' RColour)
Srx.SetValue("BackColour", BackC.ToArgb.ToString)

---------------------------------
'This is the part which loads from registry on startup.

ForeC = Drawing.Color.FromArgb(Lrx.GetValue("ForeColour").ToString)
txtConsole.ForeColor = ForeC

BackC = Drawing.Color.FromArgb(Lrx.GetValue("BackColour").ToString)
txtConsole.BackColor = BackC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top