Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

convert string to system.drawing.color

Posted on 2011-03-03
19
Medium Priority
?
1,354 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 1200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 800 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

670 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