Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making matching game for son and need some suggestions 2

Status
Not open for further replies.

chris86t

Technical User
Jun 13, 2008
41
US
I am using vb 2010 express. I have setup a windows form with 12 labels. I have made it so that when a label is clicked it will change color to show that it is selected. When 2 are selected I will check to see if they match.

I am going to populate these labels with words and definitions from a text file. There will be two columns. One for the word and one for the definition. right now I am able to set the contents of the label with the following code

Code:
For Each myControl As Control In Me.Controls
            If TypeOf myControl Is Label And Then
                CType(myControl, Label).Text = "word"
            End If
      Next myControl

The problem I'm having is that I can't come up with a way to put the words and answers into a random order. I want label2 - label7 (in column 1 on the form) to contain the words, and label8 - label13 (in column2 on the form) to contain the definitions. Here is a picture of the form:
form1.jpg


Any ideas on how to accomplish this?
 


I wouldn't worry about putting the labels on the form randomly. Just put them on the form in the 2 columns, then assign the words and definitions to them randomly. This code is for the words, but you can do the exact same thing for the definitions (just change the label number).

Code:
        Dim r As Random

        Dim i As Integer = 0

        Dim lbl As Label

        Dim bWordPlaced As Boolean = False

        For c As Integer = 1 To 6

            Do

                bWordPlaced = False

                r = New Random

                'get a random number between 2 and 7
                i = r.Next(2, 8)

                'find the label with the number between 2 and 7
                lbl = CType(GetControlByName("Label" & i.ToString), Label)

                'If the tag is blank, then this label doesn't have a word yet
                If lbl.Tag = "" Then
                    bWordPlaced = True
                    lbl.Text = "Word " & c.ToString
                    
                    'put "x" in the tag,to show this label is filled
                    lbl.Tag = "x"
                Else
                    bWordPlaced = False
                End If

                Application.DoEvents()

            Loop While Not bWordPlaced

        Next

        'clear out tags so the random placement will work again
        For c2 As Integer = 2 To 7
            lbl = CType(GetControlByName("Label" & c2.ToString), Label)
            lbl.Tag = ""
        Next

The GetControlByName function is from one of the FAQs here on Tek-Tips: faq796-5698

Code:
    Public Function GetControlByName(ByVal Name As String) As Control

        'now, why would I put a "_" in front of the name?
        Dim info As System.Reflection.FieldInfo = Me.GetType().GetField("_" & Name, _
        System.Reflection.BindingFlags.NonPublic Or _
        System.Reflection.BindingFlags.Instance Or _
        System.Reflection.BindingFlags.Public Or _
        System.Reflection.BindingFlags.IgnoreCase)

        If info Is Nothing Then Return Nothing
        Dim o As Object = info.GetValue(Me)
        Return o

    End Function

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
This works great!!!. Your method is what I had in my mind. This line:
Code:
 lbl = CType(GetControlByName("Label" & i.ToString), Label)
is what I couldn't figure out.
 

Yeah, the GetControlByName function is very handy. I use it frequently.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
The label option I've been using seems like a it won't work well for us. First there were 6 words to put into the program. Then there were 5 so I put "N/A" for one of the words and definitions. This week he brings home 8 words. I can add the extra labels to cover the extra words, but don't want to have to do this every time he has a different number of words. I was thinking of using a datagrid instead of labels.

Would a datagrid be best in this situation, or do you have a better idea?
 
Anyone have a suggestion here, or should I just use the datagrid approach?

 
Not necessarily the best idea but an easy one is you could do two ListViews and drag/drop the word to the definition or visa verse. The best would be to make your own control from the ground up that way you could totally control all aspects of it.

If you know you are never going to have more than 8 words then you could simply create the labels on the fly. The reason I don't suggest this if you have more is because a window has a max size. You could eventually simply run out of space. However we are talking about hundreds of words depending on font size so you should be OK. Below is code that you should be able to use with jebenson's example.

Code:
    Private Sub CreateWordLabels(ByVal MaxWords As Integer)
        For LabelCount As Integer = 1 To MaxWords
            Dim CurrentWordLabel As New Label
            CurrentWordLabel.Name = "Label" & LabelCount
            CurrentWordLabel.Text = "Word"
            CurrentWordLabel.AutoSize = True
            CurrentWordLabel.BackColor = Color.White

            Dim CurrentDefLabel As New Label
            CurrentDefLabel.Name = "Label" & (MaxWords + LabelCount)
            CurrentDefLabel.Text = "Definition"
            CurrentDefLabel.AutoSize = True
            CurrentDefLabel.BackColor = Color.White

            If LabelCount = 1 Then
                CurrentWordLabel.Location = New System.Drawing.Point(10, 10)
                CurrentDefLabel.Location = New System.Drawing.Point(150, 10)
            Else
                Dim LastWordLabel As Label = Me.Controls("Label" & (LabelCount - 1))
                Dim LastDefLabel As Label = Me.Controls("Label" & (MaxWords + (LabelCount - 1)))

                CurrentWordLabel.Location = New System.Drawing.Point(LastWordLabel.Location.X, LastWordLabel.Location.Y + LastWordLabel.Height)
                CurrentDefLabel.Location = New System.Drawing.Point(LastDefLabel.Location.X, LastDefLabel.Location.Y + LastDefLabel.Height)
            End If

            Me.Controls.Add(CurrentWordLabel)
            Me.Controls.Add(CurrentDefLabel)
        Next
    End Sub

You can use this anywhere before you add the words.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top