setting backcolor using a string
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: setting backcolor using a string

  1. #1
    Join Date
    Nov 2005
    Posts
    95

    setting backcolor using a string

    Hmmm... I have a string , say

    strUncleFred = "vbRed"
    or
    strUncleFred = "vbGreen"

    then I want

    textGoat.BackColor = strUncleFred

    But I get a type mismatch error. I see that .backcolor expects a long value, so I tried using
    Clng(strUncleFred) ....but that got me nowhere
    I really want to use only the simple Vb colors specified when the use types in a string.

    I can see that vbRed is a constant and "vbRed" is something else--a string, but can you convert one to the other so easy way?
    Last edited by vbcandies; December 14th, 2009 at 03:03 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,874

    Re: setting backcolor using a string

    Don't use QUOTES. That makes it a STRING by default.

    Code:
    Private Sub Form_Load()
      Dim vb As Long
      vb = vbRed
      Form1.BackColor = vb
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Nov 2005
    Posts
    95

    Re: setting backcolor using a string

    It is ALREADY being entered as a string (actually as part of a longer string), I was just showing an example.

    so if the string is "vbRed", etc how can you use it to set the color?

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,874

    Re: setting backcolor using a string

    Store colors as LONG, like I did. Try the sample. It prints 255, then changes the backcolor of the form to 255/vbRed
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: setting backcolor using a string

    David, sometimes even me does not understand what you are referring to.
    What sample prints 255 and changes the backcolor of the form to 255/vbRed???

    What the OP wanted, I think, was that a literal string like "vbRed" he has in a string variable, is to be converted to the according color value. This, I'm afraid is not possible ad hoc, since these constants are only known to the compiler, not to the runtime facility. Also the famous script control we had in discussion recently will not resolve these constants, I think.
    You can, however, write your own conversion function like this:
    Code:
    Private Function LiteralToColorValue(ColorLit As String) As Long
      Dim c As Long
      Select Case LCase(ColorLit)
        Case "vbred": c = vbRed
        Case "vbgreen": c = vbGreen
        Case "vbblue": c = vbBlue
        '...
      End Select
      LiteralToColorValue = c
    End Function
    You would have to add a Case statement for every literal you want to have converted. There are some more, I suppose, like vbWhite, vbBlack, vbYellow and so on. This is the only choice I can see at the moment.

  6. #6
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,874

    Re: setting backcolor using a string

    Quote Originally Posted by dglienna View Post
    Don't use QUOTES. That makes it a STRING by default. oops. edited out the msgbox statement

    Code:
    Private Sub Form_Load()
      Dim vb As Long
      vb = vbRed
      MsgBox vb
      Form1.BackColor = vb
    End Sub
    Turns the form RED as you step thru
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: setting backcolor using a string

    Ok. I see. And I didn't understand 255/vBred either. Sorry. Got it now.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center