Convert String to COlor

  • Thread starter Thread starter jcrouse
  • Start date Start date
J

jcrouse

I decided to start a new thread. I have output in xml file format. It looks
like this:

<P1JoyUp>
<Top>326</Top>
<Left>54</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>True</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3,
GdiCharSet=0, GdiVerticalFont=False]</Font>
</P1JoyUp>

I write it out and read it in using a data set. Now when I read it in I need
to convert the forecolor and backcolor strings to system.drawing.something.
WHere do I need to look for an answer.

Thanks,
John
 
I'm looking at the Color class and typeconverters but I dont' understand the
typedescriptor parameter or the example they give in the msdn help.

John

Scott M. said:
Have you tried converting to a color in the color class?

jcrouse said:
I decided to start a new thread. I have output in xml file format. It looks
like this:

<P1JoyUp>
<Top>326</Top>
<Left>54</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>True</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3,
GdiCharSet=0, GdiVerticalFont=False]</Font>
</P1JoyUp>

I write it out and read it in using a data set. Now when I read it in I need
to convert the forecolor and backcolor strings to system.drawing.something.
WHere do I need to look for an answer.

Thanks,
John
 
How about CType(colorString,color) ... haven't tried this though.


jcrouse said:
I'm looking at the Color class and typeconverters but I dont' understand the
typedescriptor parameter or the example they give in the msdn help.

John

Scott M. said:
Have you tried converting to a color in the color class?

jcrouse said:
I decided to start a new thread. I have output in xml file format. It looks
like this:

<P1JoyUp>
<Top>326</Top>
<Left>54</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>True</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3,
GdiCharSet=0, GdiVerticalFont=False]</Font>
</P1JoyUp>

I write it out and read it in using a data set. Now when I read it in
I
need
to convert the forecolor and backcolor strings to system.drawing.something.
WHere do I need to look for an answer.

Thanks,
John
 
Hi John,

It is possible with that string to extract it using a regex, a split or an
indexof or any combination of that.

However much easier is just to save the components of the font seperatly in
your XML file
(I showed you it the way you did to make it not that difficult explaining it
in a message, although I told that somewhere in a thread of you in this
newsgroup)

blablaFontName = textbox1.font.name.tostring
blablaFondSize = textbox1.font.size.tostring
etc.

Than setting it back is much easier
textbox1.font = new font(blablaFontName, csng(blablaFontSize))

I hope this helps?

Cor
 
Hi OHM,

I saw I did give the wrong answer on the right question.

(My message was an answer on another question from John in another thread)

(Ken did give this answer as well, so I thought it was another question)

However, thanks for correcting me.

Cor
 
I always use Color.ToARGB to write, and
Color.FromARGB to read them back again.
I think that's much easier to do...

Hope I helped,


J Mous
 
* "jcrouse said:
I decided to start a new thread. I have output in xml file format. It looks
like this:

<P1JoyUp>
<Top>326</Top>
<Left>54</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>True</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3,
GdiCharSet=0, GdiVerticalFont=False]</Font>
</P1JoyUp>

I write it out and read it in using a data set. Now when I read it in I need
to convert the forecolor and backcolor strings to system.drawing.something.
WHere do I need to look for an answer.

\\\
Me.BackColor = Color.FromName("Rosybrown")
///

- or -

\\\
Dim ColorName As String = ...
Me.Label1.BackColor = Color.FromKnownColor(DirectCast([Enum].Parse(GetType(KnownColor), ColorName), KnownColor))
///
 
Another problem. I had this working about two days ago. What am I missing
here. Here is how I write out to the xml file:

Dim dr1 As DataRow = ds.Tables("P1JoyUp").NewRow

dr1("Top") = lblP1JoyUp.Top.ToString

dr1("Left") = lblP1JoyUp.Left.ToString

dr1("Height") = lblP1JoyUp.Height.ToString

dr1("Width") = lblP1JoyUp.Width.ToString

dr1("Visible") = lblP1JoyUp.Visible.ToString

dr1("FontName") = lblP1JoyUp.Font.Name.ToString

dr1("FontSize") = lblP1JoyUp.Font.Size.ToString

dr1("FontBold") = lblP1JoyUp.Font.Bold.ToString

dr1("FontItalic") = lblP1JoyUp.Font.Italic.ToString

Here is what the xml file looks like:

<CPViewer>

<P1JoyUp>

<Top>10</Top>

<Left>10</Left>

<Height>23</Height>

<Width>100</Width>

<Visible>False</Visible>

<ForeColor>Color [White]</ForeColor>

<BackColor>Color [Transparent]</BackColor>

<FontName>Microsoft Sans Serif</FontName>

<FontSize>8.25</FontSize>

<FontBold>False</FontBold>

<FontItalic>False</FontItalic>

</P1JoyUp>


But now when I try to handle the font components individually it underlines
the four font line and says "font.xxxx property is read only. Here is the
code for reading the xml file back in:

If OpenFileDialog1.FileName <> "" Then

Dim ds As New DataSet

ds.ReadXml(OpenFileDialog1.FileName)

lblP1JoyUp.Top = ds.Tables(0).Rows(0).Item(0).ToString

lblP1JoyUp.Left = ds.Tables(0).Rows(0).Item(1).ToString

lblP1JoyUp.Height = ds.Tables(0).Rows(0).Item(2).ToString

lblP1JoyUp.Width = ds.Tables(0).Rows(0).Item(3).ToString

lblP1JoyUp.Visible = ds.Tables(0).Rows(0).Item(4).ToString

lblP1JoyUp.Font.Name = ds.Tables(0).Rows(0).Item(7).ToString

lblP1JoyUp.Font.Size = ds.Tables(0).Rows(0).Item(8).ToString

lblP1JoyUp.Font.Bold = ds.Tables(0).Rows(0).Item(9).ToString

lblP1JoyUp.Font.Italic = ds.Tables(0).Rows(0).Item(10).ToString

lblP1JoyUp.Text = lblP1JoyUp.Name


Like I said, this worked a few days ago but I can't figure it out now. Do I
need to ".ToString" when I am reading back in?

Thanks,
John
 
Well, this seems nice and simple. However, if you look at the xml file, the
color is specified as "Color [White]" and I only need the part between the
braces. If I modify the xml by hand and strip out everything but the actual
color name, this works great. So, in order to use this method, how do I
write JUST the color as a strin to the xml file.

Thanks,
John


One Handed Man ( OHM#) said:
Color.FromName("White")

OHM

jcrouse said:
I decided to start a new thread. I have output in xml file format. It looks
like this:

<P1JoyUp>
<Top>326</Top>
<Left>54</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>True</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3,
GdiCharSet=0, GdiVerticalFont=False]</Font>
</P1JoyUp>

I write it out and read it in using a data set. Now when I read it in I need
to convert the forecolor and backcolor strings to system.drawing.something.
WHere do I need to look for an answer.

Thanks,
John
 
I also like this method. It is easy to write to the xml file with this
method. Here is a sample:

<CPViewer>

<P1JoyUp>

<Top>136</Top>

<Left>99</Left>

<Height>23</Height>

<Width>100</Width>

<Visible>True</Visible>

<ForeColor>-256</ForeColor>

<BackColor>-16777216</BackColor>

<FontName>Microsoft Sans Serif</FontName>

<FontSize>8.25</FontSize>

<FontBold>False</FontBold>

<FontItalic>False</FontItalic>

</P1JoyUp>


However, I can't get the code right to read it back in. This is what I
tried, amoungst many other syntaxes:

If OpenFileDialog1.FileName <> "" Then

Dim ds As New DataSet

ds.ReadXml(OpenFileDialog1.FileName)

lblP1JoyUp.Top = ds.Tables(0).Rows(0).Item(0).ToString

lblP1JoyUp.Left = ds.Tables(0).Rows(0).Item(1).ToString

lblP1JoyUp.Height = ds.Tables(0).Rows(0).Item(2).ToString

lblP1JoyUp.Width = ds.Tables(0).Rows(0).Item(3).ToString

lblP1JoyUp.Visible = ds.Tables(0).Rows(0).Item(4).ToString

lblP1JoyUp.ForeColor.FromArgb(ds.Tables(0).Rows(0).Item(5))

lblP1JoyUp.BackColor.FromArgb(ds.Tables(0).Rows(0).Item(6))



Any idea on the reading part,

John




J M said:
I always use Color.ToARGB to write, and
Color.FromARGB to read them back again.
I think that's much easier to do...

Hope I helped,


J Mous

jcrouse said:
I decided to start a new thread. I have output in xml file format. It looks
like this:

<P1JoyUp>
<Top>326</Top>
<Left>54</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>True</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3,
GdiCharSet=0, GdiVerticalFont=False]</Font>
</P1JoyUp>

I write it out and read it in using a data set. Now when I read it in I need
to convert the forecolor and backcolor strings to system.drawing.something.
WHere do I need to look for an answer.

Thanks,
John
 
Ken......I tried your method also and failed. Read the reply above to OHM
for my attempt and results at your first recommendation. As for your second
recommendation, I don't understand the "..." at the end of the first line.

Please elaborate more,

John

Herfried K. Wagner said:
* "jcrouse said:
I decided to start a new thread. I have output in xml file format. It looks
like this:

<P1JoyUp>
<Top>326</Top>
<Left>54</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>True</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3,
GdiCharSet=0, GdiVerticalFont=False]</Font>
</P1JoyUp>

I write it out and read it in using a data set. Now when I read it in I need
to convert the forecolor and backcolor strings to system.drawing.something.
WHere do I need to look for an answer.

\\\
Me.BackColor = Color.FromName("Rosybrown")
///

- or -

\\\
Dim ColorName As String = ...
Me.Label1.BackColor =
Color.FromKnownColor(DirectCast([Enum].Parse(GetType(KnownColor),
ColorName), KnownColor))
 
* "jcrouse said:
Ken......I tried your method also and failed. Read the reply above to OHM
for my attempt and results at your first recommendation. As for your second
recommendation, I don't understand the "..." at the end of the first line.

Just replace it with a string that contains the name of the color, for
example, "Red".
 
Hi,

Dim strColor As String = "Color [White]"
Dim l As Integer = strColor.IndexOf("[")
Dim r As Integer = strColor.IndexOf("]")
Dim strFormatedColor As String = strColor.Substring(l + 1, r - l
- 1)
Dim clrBack As Color = Color.FromName(strFormatedColor)

Me.BackColor = clrBack

Ken
--------------------

jcrouse said:
Ken......I tried your method also and failed. Read the reply above to OHM

for my attempt and results at your first recommendation. As for your
second
recommendation, I don't understand the "..." at the end of the first
line.

Please elaborate more,

John

"Herfried K. Wagner [MVP]" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> wrote in message

* "jcrouse" <HYPERLINK
"mailto:[email protected]"(e-mail address removed)> scripsit:
I decided to start a new thread. I have output in xml file format.
It
looks
like this:

<P1JoyUp>
<Top>326</Top>
<Left>54</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>True</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3,
GdiCharSet=0, GdiVerticalFont=False]</Font>
</P1JoyUp>

I write it out and read it in using a data set. Now when I read it in
I
need
to convert the forecolor and backcolor strings to
system.drawing.something.
WHere do I need to look for an answer.

\\\
Me.BackColor = Color.FromName("Rosybrown")
///

- or -

\\\
Dim ColorName As String = ...
Me.Label1.BackColor =

Color.FromKnownColor(DirectCast([Enum].Parse(GetType(KnownColor),
ColorName), KnownColor))
 
Well...this seems to work:

lblP1JoyUp.ForeColor =
Color.FromArgb(ds.Tables(0).Rows(0).Item(5))

lblP1JoyUp.BackColor =
Color.FromArgb(ds.Tables(0).Rows(0).Item(6))


Does it look correct?

Thanks,
John



jcrouse said:
I also like this method. It is easy to write to the xml file with this
method. Here is a sample:

<CPViewer>

<P1JoyUp>

<Top>136</Top>

<Left>99</Left>

<Height>23</Height>

<Width>100</Width>

<Visible>True</Visible>

<ForeColor>-256</ForeColor>

<BackColor>-16777216</BackColor>

<FontName>Microsoft Sans Serif</FontName>

<FontSize>8.25</FontSize>

<FontBold>False</FontBold>

<FontItalic>False</FontItalic>

</P1JoyUp>


However, I can't get the code right to read it back in. This is what I
tried, amoungst many other syntaxes:

If OpenFileDialog1.FileName <> "" Then

Dim ds As New DataSet

ds.ReadXml(OpenFileDialog1.FileName)

lblP1JoyUp.Top = ds.Tables(0).Rows(0).Item(0).ToString

lblP1JoyUp.Left = ds.Tables(0).Rows(0).Item(1).ToString

lblP1JoyUp.Height = ds.Tables(0).Rows(0).Item(2).ToString

lblP1JoyUp.Width = ds.Tables(0).Rows(0).Item(3).ToString

lblP1JoyUp.Visible = ds.Tables(0).Rows(0).Item(4).ToString

lblP1JoyUp.ForeColor.FromArgb(ds.Tables(0).Rows(0).Item(5))

lblP1JoyUp.BackColor.FromArgb(ds.Tables(0).Rows(0).Item(6))



Any idea on the reading part,

John




J M said:
I always use Color.ToARGB to write, and
Color.FromARGB to read them back again.
I think that's much easier to do...

Hope I helped,


J Mous

jcrouse said:
I decided to start a new thread. I have output in xml file format. It looks
like this:

<P1JoyUp>
<Top>326</Top>
<Left>54</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>True</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3,
GdiCharSet=0, GdiVerticalFont=False]</Font>
</P1JoyUp>

I write it out and read it in using a data set. Now when I read it in
I
need
to convert the forecolor and backcolor strings to system.drawing.something.
WHere do I need to look for an answer.

Thanks,
John
 
jcrouse said:
But now when I try to handle the font components individually it
underlines the four font line and says "font.xxxx property is read
only. Here is the code for reading the xml file back in:

If OpenFileDialog1.FileName <> "" Then

Dim ds As New DataSet

ds.ReadXml(OpenFileDialog1.FileName)

lblP1JoyUp.Top = ds.Tables(0).Rows(0).Item(0).ToString

lblP1JoyUp.Left =
ds.Tables(0).Rows(0).Item(1).ToString

lblP1JoyUp.Height =
ds.Tables(0).Rows(0).Item(2).ToString

lblP1JoyUp.Width =
ds.Tables(0).Rows(0).Item(3).ToString

lblP1JoyUp.Visible =
ds.Tables(0).Rows(0).Item(4).ToString

lblP1JoyUp.Font.Name =
ds.Tables(0).Rows(0).Item(7).ToString

lblP1JoyUp.Font.Size =
ds.Tables(0).Rows(0).Item(8).ToString

lblP1JoyUp.Font.Bold =
ds.Tables(0).Rows(0).Item(9).ToString

lblP1JoyUp.Font.Italic =
ds.Tables(0).Rows(0).Item(10).ToString

lblP1JoyUp.Text = lblP1JoyUp.Name


Like I said, this worked a few days ago but I can't figure it out
now. Do I need to ".ToString" when I am reading back in?


You can not change Font properties. Don't assign each property, but create a
new Font and pass the font properties to the constructor:

lblp1joyup.font = new font(ds.tables(0)(0)(7).tostring, <other properties
here>)


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Hi John,

That was the answer above
Extra
Dim myfontstyle As FontStyle
If CBool(ds.Tables(0).Rows(0).Item(9)) = True Then
myfontstyle = FontStyle.Bold
End If
If CBool(ds.Tables(0).Rows(0).Item(10)) = True Then
myfontstyle = myfontstyle Or FontStyle.Italic
End If

lblP1JoyUp.Font = new font(ds.Tables(0).Rows(0).Item(7).ToString,
csng(ds.Tables(0).Rows(0).Item(8)),myfontstyle)

Maybe you should first make some extra fields. And watch typos

:-)

I hope this helps

Cor
 
John,
As I and other have commented before. If you want to read using
Color.FromName, then you need to write Color.Name. Unfortunately
Color.ToString simply will not do it for you!

Hope this helps
Jay

jcrouse said:
Well, this seems nice and simple. However, if you look at the xml file, the
color is specified as "Color [White]" and I only need the part between the
braces. If I modify the xml by hand and strip out everything but the actual
color name, this works great. So, in order to use this method, how do I
write JUST the color as a strin to the xml file.

Thanks,
John


One Handed Man ( OHM#) said:
Color.FromName("White")

OHM

jcrouse said:
I decided to start a new thread. I have output in xml file format. It looks
like this:

<P1JoyUp>
<Top>326</Top>
<Left>54</Left>
<Height>23</Height>
<Width>100</Width>
<Visible>True</Visible>
<ForeColor>Color [White]</ForeColor>
<BackColor>Color [Transparent]</BackColor>
<Font>[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3,
GdiCharSet=0, GdiVerticalFont=False]</Font>
</P1JoyUp>

I write it out and read it in using a data set. Now when I read it in
I
need
to convert the forecolor and backcolor strings to system.drawing.something.
WHere do I need to look for an answer.

Thanks,
John
 
John,
But now when I try to handle the font components individually it underlines
the four font line and says "font.xxxx property is read only. Here is the
code for reading the xml file back in:
You need to use New Font(x, y, z) where x, y, and z are the font properties
that you wrote to the file.

I mentioned this last week when you asked.

Hope this helps
Jay


jcrouse said:
Another problem. I had this working about two days ago. What am I missing
here. Here is how I write out to the xml file:

Dim dr1 As DataRow = ds.Tables("P1JoyUp").NewRow

dr1("Top") = lblP1JoyUp.Top.ToString

dr1("Left") = lblP1JoyUp.Left.ToString

dr1("Height") = lblP1JoyUp.Height.ToString

dr1("Width") = lblP1JoyUp.Width.ToString

dr1("Visible") = lblP1JoyUp.Visible.ToString

dr1("FontName") = lblP1JoyUp.Font.Name.ToString

dr1("FontSize") = lblP1JoyUp.Font.Size.ToString

dr1("FontBold") = lblP1JoyUp.Font.Bold.ToString

dr1("FontItalic") = lblP1JoyUp.Font.Italic.ToString

Here is what the xml file looks like:

<CPViewer>

<P1JoyUp>

<Top>10</Top>

<Left>10</Left>

<Height>23</Height>

<Width>100</Width>

<Visible>False</Visible>

<ForeColor>Color [White]</ForeColor>

<BackColor>Color [Transparent]</BackColor>

<FontName>Microsoft Sans Serif</FontName>

<FontSize>8.25</FontSize>

<FontBold>False</FontBold>

<FontItalic>False</FontItalic>

</P1JoyUp>


But now when I try to handle the font components individually it underlines
the four font line and says "font.xxxx property is read only. Here is the
code for reading the xml file back in:

If OpenFileDialog1.FileName <> "" Then

Dim ds As New DataSet

ds.ReadXml(OpenFileDialog1.FileName)

lblP1JoyUp.Top = ds.Tables(0).Rows(0).Item(0).ToString

lblP1JoyUp.Left = ds.Tables(0).Rows(0).Item(1).ToString

lblP1JoyUp.Height = ds.Tables(0).Rows(0).Item(2).ToString

lblP1JoyUp.Width = ds.Tables(0).Rows(0).Item(3).ToString

lblP1JoyUp.Visible = ds.Tables(0).Rows(0).Item(4).ToString

lblP1JoyUp.Font.Name = ds.Tables(0).Rows(0).Item(7).ToString

lblP1JoyUp.Font.Size = ds.Tables(0).Rows(0).Item(8).ToString

lblP1JoyUp.Font.Bold = ds.Tables(0).Rows(0).Item(9).ToString

lblP1JoyUp.Font.Italic = ds.Tables(0).Rows(0).Item(10).ToString

lblP1JoyUp.Text = lblP1JoyUp.Name


Like I said, this worked a few days ago but I can't figure it out now. Do I
need to ".ToString" when I am reading back in?

Thanks,
John



Cor Ligthert said:
Hi John,

It is possible with that string to extract it using a regex, a split or an
indexof or any combination of that.

However much easier is just to save the components of the font seperatly in
your XML file
(I showed you it the way you did to make it not that difficult
explaining
it
in a message, although I told that somewhere in a thread of you in this
newsgroup)

blablaFontName = textbox1.font.name.tostring
blablaFondSize = textbox1.font.size.tostring
etc.

Than setting it back is much easier
textbox1.font = new font(blablaFontName, csng(blablaFontSize))

I hope this helps?

Cor
 
Back
Top