none
how to display corect word my program

    Question

  • dear sir

    this is my full program.now wrong word are display.i want to display correct word how can do it.please help me.

    thank you

    wansa

    Option Strict On
    
    Public Class Form1
    
        Dim JumbledWordList As New List(Of String)
        Dim ButtonList As New List(Of Button)
        Dim BT As Button
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            Button1.Left = CInt((Me.ClientRectangle.Width / 2) - (Button1.Width / 2))
            Label1.Text = "Waiting"
            Label1.Left = CInt((Me.ClientRectangle.Width / 2) - (Label1.Width / 2))
            Label2.Text = "Waiting"
            Label2.Left = CInt((Me.ClientRectangle.Width / 2) - (Label2.Width / 2))
            Label3.AutoSize = False
            Label3.TextAlign = ContentAlignment.MiddleCenter
            Label3.Text = ""
            Label3.Left = CInt((Me.ClientRectangle.Width / 2) - (Label3.Width / 2))
            Label3.Visible = False
            Label6.Text = "0"
            Label6.BackColor = Color.Lime
            Label6.TextAlign = ContentAlignment.MiddleCenter
            Label6.MinimumSize = New Size(27, 22)
            Label6.MaximumSize = New Size(27, 22)
            Label6.Size = New Size(27, 22)
            Label7.Text = "0"
            Label7.BackColor = Color.Red
            Label7.TextAlign = ContentAlignment.MiddleCenter
            Label7.MinimumSize = New Size(27, 22)
            Label7.MaximumSize = New Size(27, 22)
            Label7.Size = New Size(27, 22)
        End Sub
    
        Dim ListCounter As Integer = 0
    
        Private Sub Start_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            ListCounter = 0
            JumbledWordList.Clear()
            ' Get list of words from file, jumble words randomly so they are out of order.
            Dim s() As String = IO.File.ReadAllLines(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Test Words 1.Txt"))
            Dim NrsToJumble = Enumerable.Range(0, s.Count)
            Dim JumbledNumbers = NrsToJumble.OrderBy(Function(a) Guid.NewGuid())
            For Each NTJ In JumbledNumbers
                JumbledWordList.Add(s(NTJ))
            Next
            ' Display first word in Label2, disable start button.
            Button1.Enabled = False
            Label1.Text = "Click on start word below to begin"
            Label1.Left = CInt((Me.ClientRectangle.Width / 2) - (Label1.Width / 2))
            Label2.Text = JumbledWordList(ListCounter)
            Label2.Visible = True
            Label2.Left = CInt((Me.ClientRectangle.Width / 2) - (Label2.Width / 2))
        End Sub
    
        Private Sub Label2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Label2.Click
            If ButtonList.Count > 0 Then
                For i = ButtonList.Count - 1 To 0 Step -1
                    Me.Controls.Remove(ButtonList(i))
                    ButtonList(i).Dispose()
                Next
            End If
            ButtonList.Clear()
            ' Jumble string of word
            Dim s As String = ""
            Dim WordsIndexesToJumble = Enumerable.Range(0, JumbledWordList(ListCounter).Count)
            Dim JumbledLetters = WordsIndexesToJumble.OrderBy(Function(a) Guid.NewGuid())
            For Each Idx In JumbledLetters
                s &= JumbledWordList(ListCounter)(Idx)
            Next
    
            Dim ButtonLocationStart As Integer = s.Count
            ButtonLocationStart = CInt((Me.ClientRectangle.Width / 2) - ((ButtonLocationStart / 2) * 42))
    
            For i = 0 To s.Count - 1
                BT = New Button
                With BT
                    .Size = New Size(40, 40)
                    .BackColor = Color.White
                    .TextAlign = ContentAlignment.MiddleCenter
                    .Text = s(i)
                    .Font = New Font("Segoe Keycaps", 16, FontStyle.Regular)
                    .Location = New Point(ButtonLocationStart, Label3.Bottom + 20)
                End With
                AddHandler BT.Click, AddressOf Buttons_Click
                ButtonLocationStart += 42
                Me.Controls.Add(BT)
                ButtonList.Add(BT)
            Next
            Label2.Text = "Please use buttons to display correct word in below Label"
            Label2.Left = CInt((Me.ClientRectangle.Width / 2) - (Label2.Width / 2))
            Dim sz = TextRenderer.MeasureText(JumbledWordList(ListCounter), Label3.Font)
            Label3.Text = ""
            Label3.Size = New Size(sz.Width + 6, sz.Height + 6)
            Label3.MinimumSize = New Size(sz.Width + 6, sz.Height + 6)
            Label3.MaximumSize = New Size(sz.Width + 6, sz.Height + 6)
            Label3.Left = CInt((Me.ClientRectangle.Width / 2) - (Label3.Width / 2))
            Label3.Visible = True
            CurrentIndex = 0
        End Sub
    
        Dim CurrentIndex As Integer = 0
        Dim RightCounter As Integer = 0
        Dim WrongCounter As Integer = 0
    
        Private Sub Buttons_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim s As String = DirectCast(sender, Button).Text
            If s = JumbledWordList(ListCounter)(CurrentIndex) Then
                Label3.Text &= s
                CurrentIndex += 1
                RightCounter += 1
                DirectCast(sender, Button).BackColor = Color.Azure
                DirectCast(sender, Button).Enabled = False
            Else
                WrongCounter += 1
            End If
            If WrongCounter = 5 Then
                For i = ButtonList.Count - 1 To 0 Step -1
                    Me.Controls.Remove(ButtonList(i))
                    ButtonList(i).Dispose()
                Next
                ButtonList.Clear()
                WrongCounter = 0
                RightCounter = 0
                Label3.Text = ""
                Label3.Visible = False
                CurrentIndex += 1
                Label6.Text = RightCounter.ToString
                Label7.Text = WrongCounter.ToString
                MessageBox.Show("Failed on word " & ChrW(34) & JumbledWordList(ListCounter) & ChrW(34) & " continue with next word.")
                ListCounter += 1
                If ListCounter < JumbledWordList.Count Then
                    For i = ButtonList.Count - 1 To 0 Step -1
                        Me.Controls.Remove(ButtonList(i))
                        ButtonList(i).Dispose()
                    Next
                    ButtonList.Clear()
                    WrongCounter = 0
                    RightCounter = 0
                    Label6.Text = RightCounter.ToString
                    Label7.Text = WrongCounter.ToString
                    Label1.Text = "Click on start word below to begin"
                    Label1.Left = CInt((Me.ClientRectangle.Width / 2) - (Label1.Width / 2))
                    Label2.Text = JumbledWordList(ListCounter)
                    Label2.Left = CInt((Me.ClientRectangle.Width / 2) - (Label2.Width / 2))
                    Label3.Text = ""
                    Label3.Visible = False
                Else
                    For i = ButtonList.Count - 1 To 0 Step -1
                        Me.Controls.Remove(ButtonList(i))
                        ButtonList(i).Dispose()
                    Next
                    ButtonList.Clear()
                    Label1.Text = "Waiting"
                    Label1.Left = CInt((Me.ClientRectangle.Width / 2) - (Label1.Width / 2))
                    Label2.Text = ""
                    Label2.Visible = False
                    Label2.Left = CInt((Me.ClientRectangle.Width / 2) - (Label2.Width / 2))
                    Label3.Text = ""
                    Label3.Visible = False
                    Button1.Enabled = True
                    MessageBox.Show("All words used. Select Button1 to restart game.")
                End If
                Exit Sub
            End If
            Label6.Text = RightCounter.ToString
            Label7.Text = WrongCounter.ToString
            If CurrentIndex = JumbledWordList(ListCounter).Count Then
                ListCounter += 1
                If ListCounter < JumbledWordList.Count Then
                    For i = ButtonList.Count - 1 To 0 Step -1
                        Me.Controls.Remove(ButtonList(i))
                        ButtonList(i).Dispose()
                    Next
                    ButtonList.Clear()
                    WrongCounter = 0
                    RightCounter = 0
                    Label6.Text = RightCounter.ToString
                    Label7.Text = WrongCounter.ToString
                    Label1.Text = "Click on start word below to begin"
                    Label1.Left = CInt((Me.ClientRectangle.Width / 2) - (Label1.Width / 2))
                    Label2.Text = JumbledWordList(ListCounter)
                    Label2.Left = CInt((Me.ClientRectangle.Width / 2) - (Label2.Width / 2))
                    Label3.Text = ""
                    Label3.Visible = False
                Else
                    For i = ButtonList.Count - 1 To 0 Step -1
                        Me.Controls.Remove(ButtonList(i))
                        ButtonList(i).Dispose()
                    Next
                    ButtonList.Clear()
                    Label1.Text = "Waiting"
                    Label1.Left = CInt((Me.ClientRectangle.Width / 2) - (Label1.Width / 2))
                    Label2.Text = ""
                    Label2.Visible = False
                    Label2.Left = CInt((Me.ClientRectangle.Width / 2) - (Label2.Width / 2))
                    Label3.Text = ""
                    Label3.Visible = False
                    Button1.Enabled = True
                    WrongCounter = 0
                    RightCounter = 0
                    Label6.Text = RightCounter.ToString
                    Label7.Text = WrongCounter.ToString
                    MessageBox.Show("All words used. Select Button1 to restart game.")
                End If
            End If
        End Sub
    
    End Class
    

    Wednesday, March 22, 2017 1:31 AM

All replies

  • i want to display correct word how can do it.

    Your Buttons_Click sub is too large and complex.

    Break the routines in that sub out into separate methods (Subs and Functions). Then insert  breakpoints at the start of each method, and follow the sequence in which the methods get called.  Add Watches for the counter and the current index.  You should be able to see the problem in the flow of the code that is causing the counter to change before the user has completed their entry. You should rename your controls so that the names are meaningful - with the current code it is too difficult to connect a line of code to a particular control as displayed.

    Wednesday, March 22, 2017 2:04 AM
  • Dear Sir

    thank you, your advice will try to do it.

    thank you

    wansa

    Wednesday, March 22, 2017 12:48 PM