Results 1 to 6 of 6

Thread: [RESOLVED] Check backcolor color of object

  1. #1

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Resolved [RESOLVED] Check backcolor color of object

    Simple question...is it possible to check the backcolor of an object, like a label for instance?

    What I want to do is check if a label's backcolor is green (or some other color) then have something else happen based off the color.

    Here is what I tryed, but neither worked...

    Code:
    if label1.backcolor=color.green then
    'stuff goes here
    end if
    Code:
    if label1.backcolor.green then
    'stuff goes here
    end if
    Last edited by Shadow45o; May 23rd, 2009 at 12:53 PM.

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Check backcolor color of object

    How do you mean didn't work?

    Your second snippet certainly isn't going work, but the first one should work fine.

    I've tested it to make sure I'm not going mad. Created a form, added a label and set its back color to black, then tried this code

    Code:
            If Label1.BackColor = Color.Black Then
                MsgBox("yep its black OK")
            Else
                MsgBox("It isn't black")
            End If
    To which it dutifully informed me that the label's backcolor was indeed black.

    The obvious question is : are you totally sure that the background color of your label is green? (and by that I mean the combination of RGB values that VB recognises as green).

  3. #3

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Re: Check backcolor color of object

    It shows the error:
    "operator '=' is not defined for types 'System.Drawing.Color' and 'System.Drawing.Color'"

    So i don't if it is just a stupid mistake or why it isn't working...

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Check backcolor color of object

    Quote Originally Posted by Shadow45o View Post
    It shows the error:
    "operator '=' is not defined for types 'System.Drawing.Color' and 'System.Drawing.Color'"

    So i don't if it is just a stupid mistake or why it isn't working...
    Hmm.. that doesn't make sense. You are not doing anything daft - that should work.

    Not sure what to suggest to be honest.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check backcolor color of object

    The '=' operator only exists for a type if it has been defined in that type. '=' is defined for the Color type from .NET 2.0 but it isn't in .NET 1.x. Try using the Equals method instead:
    vb.net Code:
    1. If Me.Label1.BackColor.Equals(Color.Green) Then
    In future, please provide ALL the information in the first post. The code you used and the error message it produced are always relevant.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Re: Check backcolor color of object

    Quote Originally Posted by jmcilhinney View Post
    In future, please provide ALL the information in the first post. The code you used and the error message it produced are always relevant.
    Ok, and thanks, it works 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
  •  



Click Here to Expand Forum to Full Width