Saving the Color value into a string

H

Henry Jones

VB.NET Windows Forms VS 2005

I would like to give the user the option of changing the background color of
the textbox to anything they want. Can someone provide code how to save the
textbox1.BackColor to a string and then convert it back to a color that I
can assign to the textbox?

I have looked at colorconverter class which can inherit from the
TypeConverter class and it's all too confusing. I tried some examples and
just can't get it.

I tried something like this:

Dim myColor As Color = Color.PaleVioletRed

' Create the ColorConverter.
Dim converter As System.ComponentModel.TypeConverter = _
System.ComponentModel.TypeDescriptor.GetConverter(myColor)


and it works if the color is already set, but what if I want the user to
choose from the Color Dialog box?

HELP please
 
R

RobinS

Why do you want to convert it to a string?

If you're trying to save the setting so you can apply it
later when they run the app again, you can save it as a
color type.

Robin S.
 
M

me

Henry said:
VB.NET Windows Forms VS 2005

I would like to give the user the option of changing the background color of
the textbox to anything they want. Can someone provide code how to save the
textbox1.BackColor to a string and then convert it back to a color that I
can assign to the textbox?

I have looked at colorconverter class which can inherit from the
TypeConverter class and it's all too confusing. I tried some examples and
just can't get it.

I tried something like this:

Dim myColor As Color = Color.PaleVioletRed

' Create the ColorConverter.
Dim converter As System.ComponentModel.TypeConverter = _
System.ComponentModel.TypeDescriptor.GetConverter(myColor)


and it works if the color is already set, but what if I want the user to
choose from the Color Dialog box?

HELP please
Use the common dialog for color full open style to select a color

for use of the color between runs, create registry entry for your
program under current user/software/programname/startup/textbox1color
etc... or create your own ini file.

save all settings before exit... load all on startup
 
G

Guest

Why a string?
just keep it as a color
or if you must there is always Color.ToString
or more sensibly
color.FromARGB / Color.ToARGB convert colours To/From integers
and if you ***really*** need it as a string use the above to get the integer
and then convert that to a string (although I cannot see the point)

Guy
 
H

Henry Jones

I wanted to convert it to a string to keep the users choice in a
configuration file with some other options. How can I save it as a color
type to a table?

Thanks,

Henry
 
H

Henry Jones

I have a table with user options that is why I wanted to store the color
options as a string.
I thought if I converted the value to a string, it would be easy to convert
back. But no, it's not.

Thanks,

Henry
 
H

Henry Jones

Thanks for the info, but how can I keep it as a color in a table? I will
look up the Color options you suggested.

Henry
 
R

RobinS

You can save this information to the <Settings>
information for the project.

You can add this info by double-clicking on "my project"
in the solution explorer. This brings up the project
properties. Click on the <Settings> tab.

Add the settings here. Each setting must have a
unique name, such as ProductTextBoxColor,
CustomerTextBoxColor, etc.

For Type, set it to System.Drawing.Color.

Set the scope to User.

Then in your code, when the user changes it, save it to
the settings:

My.Settings.ProductTextBoxColor = Color.PaleVioletRed

Then when you want to use it, use My.Settings.ProductTextBoxColor
instead of a color. It will retrieve the value from the Settings
and use it.

ProductTextBox.BackColor = My.Settings.ProductTextBoxColor

The .Net runtime saves the settings when the application is
closed, and reads them when it is opened.

You can use the ColorDialog to allow the user to select
a color. I would save it to My.Settings at that point,
then turn around and assign it using My.Settings.ProductTextBoxColor;
this way you can ensure that it's working properly.

Robin S.
 

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

Top