Hello again. I am back with the problem of using booleans with command buttons. Any reasearch I've done for other methods to complete my memory game have been for Vb2005, VB.net, VB2010, etc. Where as I'm stuck with VB5 not Knowing what to do.

 `PLEASE NOTE: boolFirst is declared and defined in Form_Load!`

 Public Sub cmd1_Click()
            Dim bool1 As Boolean
            cmd1.Caption = 1

            If bool16 = True & boolFirst = True Then `If cmd16 has been clicked first`
                cmd1.Visible = False
                cmd16.Visible = False
                lbl1.Visible = True
                lbl16.Visible = True
            ElseIf bool16 = False & boolFirst = True Then `If any other button was clicked first`
                cmd1.Caption = "?"
                bool1 = False
                boolFirst = False
            Else `Any other scenario`
                bool1 = True
                boolFirst = True
            End If
        End Sub

        Public Sub cmd16_Click()
            Dim bool16 As Boolean
            cmd16.Caption = 1

            If bool1 = True & boolFirst = True Then `If cmd1 has been clicked first`
                cmd1.Visible = False
                cmd16.Visible = False
                lbl1.Visible = True
                lbl16.Visible = True
            ElseIf bool1 = False & boolFirst = True Then `If any other button was clicked first`
                cmd16.Caption = "?"
                bool16 = False
                boolFirst = False
            Else  `Any other scenario`
                bool16 = True
                boolFirst = True
            End If
        End Sub

When running this, If I click on cmd1 first, the caption changes to "1" as expected, and it seems to be working. However upon clicking cmd16, teh caption changes, like for cmd1, however the buttons do not go away. Additionally, If I click any other button first, (and therefore setting boolFirst to true, but not bool16) the caption for cmd1 is set to "1", but not immeadiatly set back to "?".

The (apparent) problem here is that the if statements are not doing what I want them to do. Apart from the caption changing, nothing appears to work. My guess here is that where my booleans are declared are the problem, however switching around their locations has not solved the problem. Another possibility is messy code, and due to me beign reletively new to VB, I would not be surprised. any solutions out there for me to try?

Recommended Answers

All 2 Replies

Here is a problem of scope of the variables.
Like boolFirst, all boolean variables like bool1, bool2,.......bool16 must declare at Formlevel.

If bool16 = True & boolFirst = True Then
ElseIf bool16 = False & boolFirst = True Then
If bool1 = True & boolFirst = True Then
ElseIf bool1 = False & boolFirst = True Then

In vb the use of "&" is to concatinate two string value.Not as operator. Use "And" operator to check the condition.

Hope it should help you

I suggest you name your booleans for the condittion they represent. Also, you don't need to do an explicit comparison. For example, if you have a boolean named EndOfFile, instead of

If EndOfFile = True Then

you can do

If EndOfFile Then

Your code becomes more concise and with proper naming it becomes much clearer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.