Results 1 to 7 of 7

Thread: Visual Studio 2012 Question/Help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    21

    Visual Studio 2012 Question/Help

    Hi, so im making a Connect Four game, but im having trouble with the declaring the winner part. I have the pieces as back colors in picture boxes, but i cant figure out how to write the if statement saying if four picture boxes have the same back color.

    for example i currently have:

    Function Winner()
    For rows = 0 To 5
    For columns = 0 To 2
    If picboxarray(rows, columns).BackColor = picboxarray(rows, columns + 1).BackColor And picboxarray(rows, columns + 1).BackColor = picboxarray(rows, columns + 2).BackColor And picboxarray(rows, columns + 2).BackColor And picboxarray(rows, columns + 3).BackColor And picboxarray(rows, columns).BackColor <> Nothing Then
    Return True
    End If
    Next
    Next
    End Function


    i get the error: Operator 'And' is not defined for types 'Boolean' and 'System.Drawing.Color'.

    Any ideas on an alternative way? Thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Visual Studio 2012 Question/Help

    is this what you intended?:

    Code:
    If picboxarray(rows, columns).BackColor = picboxarray(rows, columns + 1).BackColor And picboxarray(rows, columns + 1).BackColor = picboxarray(rows, columns + 2).BackColor And picboxarray(rows, columns + 2).BackColor <> Nothing And picboxarray(rows, columns + 3).BackColor <> Nothing And picboxarray(rows, columns).BackColor <> Nothing Then
        Return True
    End If

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Visual Studio 2012 Question/Help

    This line is an interesting one:

    And picboxarray(rows, columns).BackColor <> Nothing Then

    If .Backcolor is actually nothing, then ALL of the previous conditions will throw exceptions, so this particular part of the If statement had better be true. The better way to write that would be to put that part first, and use AndAlso rather than And. If you use And, then all parts of the expression will be evaluated. By using AndAlso, if the first part is False, then the rest aren't evaluated because the total result is already known.

    As for the actual problem, you started out ok on that If statement, but then there is this part in the middle:

    And picboxarray(rows, columns + 2).BackColor And

    Note that it starts and ends with And, with no = sign in the middle. That is not what you meant to write, as you did it correctly for the first two by comparing the BackColor to some other color.
    My usual boring signature: Nothing

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Visual Studio 2012 Question/Help

    Actually a picturebox BackColor can never be nothing. If you set it to Nothing it will revert to default (usually Color[Control]) so that part of the condition is useless as it stands.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Visual Studio 2012 Question/Help

    It will revert to the default value for the Color structure, which should be Black, but now that I think about it, you can't compare anything to Nothing using <> anyways. You'd have to use Is Not. Of course, compilation never got that far because of the missing part of the statement from earlier.
    My usual boring signature: Nothing

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: Visual Studio 2012 Question/Help

    using a picturebox straight from the toolbox with no modifications:

    Code:
    MsgBox(PictureBox1.BackColor <> Nothing) '=true
    MsgBox(PictureBox1.BackColor <> SystemColors.Control) '=false

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    21

    Re: Visual Studio 2012 Question/Help

    thanks everyone i found out my problem. feel free to check out my new thread once it gets accepted to be put up

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