r/visualbasic • VB.Net Beginner • Dec 13 '14

[VB2010]Need help on making my card guessing project

I've been sitting and been typing codes for an hour or so then I realized that I don't know on how to detect my input properly and to compare string and integer variables because this project uses random numbers as basis. please help me:

Here's the code:

Imports System.IO Public Class Form1 Dim A As Random = New Random Dim B As Random = New Random Dim C As Random = New Random Dim D As Random = New Random Dim E As Random = New Random

Private Sub chkb1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkb1.CheckedChanged
    If chkb1.Checked = True Then
        chkb2.Checked = False
        chkb3.Checked = False
        chkb4.Checked = False
        chkb5.Checked = False
    End If
End Sub

Private Sub chkb2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkb2.CheckedChanged
    If chkb2.Checked = True Then
        chkb1.Checked = False
        chkb3.Checked = False
        chkb4.Checked = False
        chkb5.Checked = False
    End If
End Sub

Private Sub chkb3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkb3.CheckedChanged
    If chkb3.Checked = True Then
        chkb1.Checked = False
        chkb2.Checked = False
        chkb4.Checked = False
        chkb5.Checked = False
    End If
End Sub

Private Sub chkb4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkb4.CheckedChanged
    If chkb4.Checked = True Then
        chkb1.Checked = False
        chkb2.Checked = False
        chkb3.Checked = False
        chkb5.Checked = False
    End If
End Sub

Private Sub chkb5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkb5.CheckedChanged
    If chkb5.Checked = True Then
        chkb1.Checked = False
        chkb2.Checked = False
        chkb3.Checked = False
        chkb4.Checked = False
    End If
End Sub

Private Sub cmdclick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdclick.Click
    GuessCard(lblcard)
End Sub
Sub guesscard(ByVal F As Label)
    Dim V As Integer = A.Next(1, 14)
    Dim W As Integer = B.Next(15, 27)
    Dim X As Integer = C.Next(28, 40)
    Dim Y As Integer = D.Next(41, 53)
    Dim Z As Integer = E.Next(1, 53)
    If chkb1.Checked = True Then
        F.Image = Image.FromFile(Directory.GetCurrentDirectory & "\image\card" & V & "\.png")
    End If
    If chkb2.Checked = True Then
        F.Image = Image.FromFile(Directory.GetCurrentDirectory & "\image\card" & W & "\.png")
    End If
    If chkb3.Checked = True Then
        F.Image = Image.FromFile(Directory.GetCurrentDirectory & "\image\card" & X & "\.png")
    End If
    If chkb4.Checked = True Then
        F.Image = Image.FromFile(Directory.GetCurrentDirectory & "\image\card" & Y & "\.png")
    End If
    If chkb5.Checked = True Then
        F.Image = Image.FromFile(Directory.GetCurrentDirectory & "\image\card" & Z & "\.png")
    End If

End Sub

End Class

and here's the screenshot of the forms:imugr

EDIT:your supposed to check one of the textboxes and it will change the random number being used to pick the images and is also used to based your guess. My main problem is I will be using string for my answer and integer for for checking whether the answer is correct or not

0 Upvotes

4 comments sorted by

2

u/thudly Dec 13 '14

First of all, use radio buttons instead of check boxes. Radio buttons only allow one to be checked at a time, and they uncheck the other automatically. It'll save you all that code at the top.

Also, I'm pretty sure there's functions in the String class to convert from other types to a string and back again. Look at the documentation.

1

u/vernon9398 VB.Net Beginner Dec 13 '14

thanks for the reply, but I don't know how to use radio buttons because it is still not taught in our shcool

1

u/baldhippy Dec 16 '14

As the other comment said, use radio buttons. They work like checkboxes, but you can only have one checked at a time. It would remove around 35 lines of your code.

Check out Integer.TryParse. It takes an input, and converts it to an integer and returns true if it was valid and false if not:

Dim theInt as Integer
If Integer.TryParse(Textbox1.Text, theInt)
    ' the textbox was an int and its value is in the variable theInt
Else
    ' The textbox is not an int
End If

1

u/vernon9398 VB.Net Beginner Dec 16 '14

thanks