Solved

convert string to system.drawing.color

Posted on 2011-03-03
19
1,344 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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