Results 1 to 4 of 4

Thread: Show and hide picture

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    2

    Smile Show and hide picture

    Hi everyone!

    Just playing around with VB and have made a program for playing "pairs". The program displays lots of face down cards, then the user can click on them to reveal their face and try to find pairs.

    If the user finds a pair, the cards should stay face up, however if they do not match, they should be returned to facing down. This needs to be done by hiding or showing the "reverse of card" picture

    I have tried to do this by using
    Code:
    play.carddown(0).Visible = True
    in the "Else" block in the "comparecard" function, but it returns errors!

    What do I need to do?

    The pictures need to be loaded dynamically through an array...

    The code I have so far is as follows...

    Form1...

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            play.Show()
            Me.Close()
        End Sub
    End Class
    play.vb...

    Code:
    'Imports mysql.Data.MySqlClient
    Public Class play
        Public carddown(52) As System.Windows.Forms.PictureBox
        Public cardup(52) As System.Windows.Forms.PictureBox
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            For i As Integer = 1 To 52
                carddown(i) = New System.Windows.Forms.PictureBox()
                carddown(i).Image = System.Drawing.Image.FromFile("N:\CS2TX6\spring\YamalaCard\0.png")
                carddown(i).Height = 100
                carddown(i).Width = 71
    
                If i = 1 Then
                    carddown(i).Top = 100
                    carddown(i).Left = 29
                ElseIf i > 1 And i < 14 Then
                    carddown(i).Top = 100
                    carddown(i).Left = carddown(i - 1).Left + carddown(i - 1).Width + 5
                ElseIf i = 14 Then
                    carddown(i).Top = 211
                    carddown(i).Left = 29
                ElseIf i > 14 And i < 27 Then
                    carddown(i).Top = 211
                    carddown(i).Left = carddown(i - 1).Left + carddown(i - 1).Width + 5
                ElseIf i = 27 Then
                    carddown(i).Top = 317
                    carddown(i).Left = 29
                ElseIf i > 27 And i < 40 Then
                    carddown(i).Top = 317
                    carddown(i).Left = carddown(i - 1).Left + carddown(i - 1).Width + 5
                ElseIf i = 40 Then
                    carddown(i).Top = 423
                    carddown(i).Left = 29
                ElseIf i > 40 And i < 53 Then
                    carddown(i).Top = 423
                    carddown(i).Left = carddown(i - 1).Left + carddown(i - 1).Width + 5
                Else
                End If
                AddHandler carddown(i).Click, AddressOf Me.carddown_click
                Me.Controls.Add(carddown(i))
            Next
    
            shuffle()
    
            For n = 0 To 51
    
                cardup(n) = New System.Windows.Forms.PictureBox()
                cardup(n).Height = 100
                cardup(n).Width = 71
    
                cardup(n).Image = System.Drawing.Image.FromFile("N:\CS2TX6\spring\YamalaCard\cardface" & "\" & nArr(n) & ".jpg")
                cardup(n).Name = "Card " + n.ToString()
                If n = 0 Then
                    cardup(n).Top = 100
                    cardup(n).Left = 29
                ElseIf n > 0 And n < 13 Then
                    cardup(n).Top = 100
                    cardup(n).Left = cardup(n - 1).Left + cardup(n - 1).Width + 5
                ElseIf n = 13 Then
                    cardup(n).Top = 211
                    cardup(n).Left = 29
                ElseIf n > 13 And n < 26 Then
                    cardup(n).Top = 211
                    cardup(n).Left = cardup(n - 1).Left + cardup(n - 1).Width + 5
                ElseIf n = 26 Then
                    cardup(n).Top = 317
                    cardup(n).Left = 29
                ElseIf n > 26 And n < 39 Then
                    cardup(n).Top = 317
                    cardup(n).Left = cardup(n - 1).Left + cardup(n - 1).Width + 5
                ElseIf n = 39 Then
                    cardup(n).Top = 423
                    cardup(n).Left = 29
                ElseIf n > 39 And n < 52 Then
                    cardup(n).Top = 423
                    cardup(n).Left = cardup(n - 1).Left + cardup(n - 1).Width + 5
                Else
                End If
                cardup(n).Tag = n.ToString()
                AddHandler cardup(n).Click, AddressOf Me.cardselect_click
                Me.Controls.Add(cardup(n))
            Next
    
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            AboutBox1.Show()
        End Sub
    
        Public Sub carddown_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim Parent = sender
            parent.visible = False
        End Sub
    
        Public Sub cardselect_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
            Dim grandparent = sender
            Dim TempInt = grandparent.Tag()
            grandparent.visible = True
            Dim arrayindex As VariantType
            arrayindex = TempInt
            MsgBox("Card " & TempInt & " clicked " & nArr(arrayindex))
    
            MsgBox(comparecard(nArr(arrayindex), CardTwo()))
            'If comparecard() = "same" Then
            '    Parent.visible = True
            'Else
            '    Parent.visible = False
            'End If
    
        End Sub
    
    End Class
    module2.vb...

    Code:
    Module Module2
    
    
        Public secondcard As String
        Public nArr(51) As Long
    
        Function shuffle()
            Dim n As Long
            Dim nTemp As Long, nRand As Long
    
            For n = 0 To 51
                nArr(n) = n + 1
            Next
    
            Randomize()
            For n = 52 To 1 Step -1
                nRand = Int(Rnd() * n)
                nTemp = nArr(nRand)
                nArr(nRand) = nArr(n - 1)
                nArr(n - 1) = nTemp
            Next
        End Function
        Function CardTwo() As Boolean
    
            Static firstcard
            If firstcard Then
                CardTwo = True
            Else
                CardTwo = False
            End If
            firstcard = Not (firstcard)
        End Function
    
    
        Public Function comparecard(ByRef nArr As String, ByVal Check As Boolean) As String
    
            Static lastcard
            Dim cardvalue
            comparecard = "NA"
            cardvalue = nArr
            If Check Then
                comparecard = IIf((cardvalue = lastcard + 26 Or cardvalue = lastcard - 26), "Same", "Different")
                lastcard = cardvalue
                'For i As Integer = 1 To 26
                '    form1.Label3.Text = ("", (i))
                'Next
            Else
                lastcard = cardvalue
                'play.carddown_click() = True
                play.carddown(0).Visible = False
            End If
    
        End Function
    
    
        'Public Function comparecard(ByRef nArr As String, ByVal Check As Boolean) As String
    
        '    Static lastcard
        '    comparecard = "NA"
        '    If nArr = lastcard + 26 Or nArr = lastcard - 26 Then
        '        MsgBox("Match")
        '        carddown(firstcard).Visible = False
        '        cardup(firstcard).Visible = True
        '        carddown(secondcard).Visible = False
        '        cardup(secondcard).Visible = True
        '    Else
        '        MsgBox("Different")
        '        carddown(firstcard).Visible = False
        '        carddown(secondcard).Visible = True
        '    End If
        'End Function
    
        'Public Function Divide(ByVal n1 As Integer, ByVal n2 As Integer) As Object
        '    If n2 = 0 Then
        '        MsgBox("Cannot divide by 0")
        '    Else
        '        MsgBox(n1 / n2)
        '    End If
    
        'End Function
    
        'Dim connectionString As String = "SERVER=mysql.sse.reading.ac.uk;DATABASE=CS2TX6;Uid=;Pwd="
        'Dim conn As New MySqlConnection(connectionString)
    
        'Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '    conn.Open()
        '    Dim cmd As MySqlCommand
        '    cmd = New MySqlCommand("INSERT INTO registrations (user_id,reg_machine) VALUES (?uid,?regm)", conn)
        '    cmd.Parameters.Add("?uid", MySqlDbType.VarChar, 40, "user_id")
        '    cmd.Parameters.Add("?regm", MySqlDbType.VarChar, 40, "reg_machine")
        '    Dim id As Integer
        '    id = get_user_id("test user2")
        '    cmd.Parameters("?uid").Value = id
        '    cmd.Parameters("?regm").Value = "my PC serial"
        '    cmd.ExecuteNonQuery()
        'End Sub
    
        'Private Function get_user_id(ByVal name As String)
        '    Dim da As New MySqlDataAdapter("Select id from users where name=?name", conn)
        '    da.SelectCommand.Parameters.Add("?name", MySqlDbType.VarChar, 40, "name")
        '    da.SelectCommand.Parameters("?name").Value = name
        '    Dim ds As New DataSet("users")
        '    da.Fill(ds)
        '    Dim t As DataTable = ds.Tables(0)
        '    Return t.Rows(0).Item("id")
        'End Function
    End Module
    Thanks for your help everyone!

    James

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Show and hide picture

    James, welcome to the forums.

    Unfortunately, you posted a .Net project in the VB6 forums. The moderators will move this thread for you.

    But I'll give it a quick shot as long as it is here. It appears you are creating an array of cards from 1 to 52, however, you are calling carddown(0). Does changing it to a non-zero index help?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    2

    Talking Re: Show and hide picture

    I've tried
    Code:
    play.carddown(3).Visible = True
    But it didn't do anything to card 3

    All I need to do is make the picture of the card back visible if the cards are different, is it really that difficult!? lol

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Show and hide picture

    Hi James,

    I notice you defined your packs as carddown(52) and cardup(52). That means you have two 53 card packs.

    The For loop goes from 1 to 52, which won't go out of range in a 53 card pack. But the first card, carddown(0), is not instantiated. That's probably why you get an error. And card(3) refers to the fourth card in the pack, which may not be the card you are showing. So I think you need to take a good look at your code with that in mind.

    bye, BB

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