Solved

convert string to system.drawing.color

Posted on 2011-03-03
19
1,342 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

23 Experts available now in Live!

Get 1:1 Help Now