Solved

convert string to system.drawing.color

Posted on 2011-03-03
19
1,339 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
  • 7
  • 6
  • 5
19 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
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
Comment Utility
It's tedious, but I'm fairly sure that's the only way to do it.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
Comment Utility
>>  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
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
Cool. Missed the FromName thing. Ignore me.
0
 

Author Comment

by:mgmhicks
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
>>   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
Comment Utility
I removed it from the rows in the table.  So the column only has red, green, in it.
0
 

Author Comment

by:mgmhicks
Comment Utility
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
Comment Utility
Step through the code and see what c equals.
Or do MsgBox(c) right after you set it to see what it equals.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:mgmhicks
Comment Utility
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
Comment Utility
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
Comment Utility
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 74

Expert Comment

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

Expert Comment

by:TommySzalapski
Comment Utility
Um, kaufmed should have gotten most of the points.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
It's cool. It happens. No worries  = )
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now