Solved

convert string to system.drawing.color

Posted on 2011-03-03
19
1,351 Views
Last Modified: 2012-05-11
drawing a blank here. I have a table with a column colled color and it has something like color.lightblue in it.  What I need to do is convert that to a the system.drawing.color I need to change the backcolor of a tab page.  Here is what I am trying but not working

For i = 0 To myDS.Tables(0).Rows.Count - 1
            Dim TabPage As New TabPage
            TabPage.Text = myDS.Tables(0).Rows(i).Item(1).ToString
            TabPage.Tag = myDS.Tables(0).Rows(i).Item(0).ToString
            TabPage.BackColor = CType(myDS.Tables(0).Rows(i).Item(3), System.Drawing.Color)            tabInspection.TabPages.Add(TabPage)
        Next i
0
Comment
Question by:mgmhicks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 5
19 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35030661
You'll just have to make a table and do the conversion yourself.
Something like
switch(colorString)
{
  case("lightblue")
    return System.Drawing.Color.LightBlue;
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35030669
It's tedious, but I'm fairly sure that's the only way to do it.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
ID: 35030763
>>  You'll just have to make a table and do the conversion yourself.

Oh ye of little faith...



Try this out. You would need to chop off the "Color." part and just use the common (user-friendly) name:
Dim c As Color = Color.FromName(Me.TextBox1.Text)

Me.BackColor = c

Open in new window

untitled.PNG
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35030845
Cool. Missed the FromName thing. Ignore me.
0
 

Author Comment

by:mgmhicks
ID: 35030923
Here is the code.  No errors but no change of color either?  Any ideas.  The dataset record column now has red, lightblue, etc  and when I do a break c is the color.aqua or whatever so it seems to be changing the value, but not the color.

Private Function FillTabs() As Boolean


        Dim myDS As New DataSet
        Dim i As Integer = 0
        myDS.ReadXml("C:\AptInspections\Categories.xml")

        For i = 0 To myDS.Tables(0).Rows.Count - 1
            Dim c As Color = Color.FromName(myDS.Tables(0).Rows(i).Item(3).ToString)
            Dim TabPage As New TabPage
            TabPage.Text = myDS.Tables(0).Rows(i).Item(1).ToString
            TabPage.Tag = myDS.Tables(0).Rows(i).Item(0).ToString
            TabPage.BackColor = c
            tabInspection.TabPages.Add(TabPage)
        Next i

    End Function

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35030966
>>   No errors but no change of color either?

Remember I said you'd have to "chop-off" the "color." part. Try this modification:
Private Function FillTabs() As Boolean


        Dim myDS As New DataSet
        Dim i As Integer = 0
        myDS.ReadXml("C:\AptInspections\Categories.xml")

        For i = 0 To myDS.Tables(0).Rows.Count - 1
            Dim c As Color = Color.FromName(myDS.Tables(0).Rows(i).Item(3).ToString().ToLower().Replace("color.", String.Empty))
            Dim TabPage As New TabPage
            TabPage.Text = myDS.Tables(0).Rows(i).Item(1).ToString
            TabPage.Tag = myDS.Tables(0).Rows(i).Item(0).ToString
            TabPage.BackColor = c
            tabInspection.TabPages.Add(TabPage)
        Next i

    End Function

Open in new window

0
 

Author Comment

by:mgmhicks
ID: 35030977
I removed it from the rows in the table.  So the column only has red, green, in it.
0
 

Author Comment

by:mgmhicks
ID: 35031035
Whats happen is the button buttons on the tabpage are turning the color, the last color sent, but the tabpage  backcolor has not changed.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35031121
Step through the code and see what c equals.
Or do MsgBox(c) right after you set it to see what it equals.
0
 

Author Comment

by:mgmhicks
ID: 35031153
c      "{Name=yellow                   , ARGB=(0, 0, 0, 0)}"      System.Drawing.Color

thats what it looks like in watch window.  When I click the plus+ sign on c I can see all the colors.

0
 

Author Comment

by:mgmhicks
ID: 35031218
If I type tabpage.backcolor = color.aqua  it works, but I cant hard code them, they have to be different colors.
0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 200 total points
ID: 35031281
If you give it an invalid color, it just creates a new one. Try trimming the value (I see whitespace).
Dim c As Color = Color.FromName(myDS.Tables(0).Rows(i).Item(3).ToString().ToLower().Replace("color.", String.Empty).Trim())
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35031307
I'm speechless...
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35031311
Um, kaufmed should have gotten most of the points.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35031316
It's cool. It happens. No worries  = )
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35031386
In the interest of helping future viewers, this is what I was working on in the interim:


Sample XML
<?xml version="1.0" encoding="utf-8" ?>
<dataset>
  <row>
    <tag>Greeting</tag>
    <text>Hello World!</text>
    <misc>nothing</misc>
    <color>lightblue</color>
  </row>
  <row>
    <tag>Salutation</tag>
    <text>Goodbye World!</text>
    <misc>nothing</misc>
    <color>purple</color>
  </row>
  <row>
    <tag>Extra</tag>
    <text>Eat At Joe's!</text>
    <misc>nothing</misc>
    <color>magenta</color>
  </row>
</dataset>

Open in new window



Code
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FillTabs()
    End Sub

    Private Function FillTabs() As Boolean
        Dim myDS As New DataSet
        Dim i As Integer = 0
        myDS.ReadXml("Categories.xml")

        For i = 0 To myDS.Tables(0).Rows.Count - 1
            Dim c As Color = Color.FromName(myDS.Tables(0).Rows(i).Item(3).ToString())
            Dim TabPage As New TabPage
            TabPage.Text = myDS.Tables(0).Rows(i).Item(1).ToString
            TabPage.Tag = myDS.Tables(0).Rows(i).Item(0).ToString
            TabPage.BackColor = c
            tabInspection.TabPages.Add(TabPage)
        Next i

    End Function
End Class

Open in new window

untitled.PNG
0
 

Author Comment

by:mgmhicks
ID: 35031428
kaufmed you are right you should have gotten points.  Little excited it worked!  I am trying to figure out if I can get you some.  

thanks
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35031609
I already hit 'request attention' so a moderator should check in soon. You could hit it too, though. They are more likely to reopen at the request of the asker.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question