By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,270 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,270 IT Pros & Developers. It's quick & easy.

Changing a richtextbox's colour!

P: n/a
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.

Feb 9 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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.
Feb 9 '06 #2

P: n/a
>
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

Feb 9 '06 #3

P: n/a
"Hugh Janus" <my*************@hotmail.com> schrieb:
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")))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Feb 9 '06 #4

P: n/a
> \\\
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("F oreColor")))
Console.BackColor =
System.Drawing.Color.FromName(CInt(Lrx.GetValue("B ackColor")))

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

Feb 9 '06 #5

P: n/a
"Hugh Janus" <my*************@hotmail.com> schrieb:
Me.RichTextBox1.BackColor = _
Color.FromName(CInt(Lrx.GetValue("ForeColor")))
///


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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Feb 9 '06 #6

P: n/a
> 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

Feb 9 '06 #7

P: n/a
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.

"Hugh Janus" <my*************@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

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

Feb 9 '06 #8

P: n/a
Hang on a second,

Try changing the name of your rich textbox to something other than
Console...

"Nick Pateman" <a@a.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.

"Hugh Janus" <my*************@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
>
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


Feb 9 '06 #9

P: n/a
> > 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.


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

Feb 9 '06 #10

P: n/a
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.

Feb 9 '06 #11

P: n/a
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

Feb 9 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.