LOOP PROBLEMS in VB.Net - vb.net

I am creating a math tutorial app for kids in the primary level. And I am having a hard time in creating a loop that would generate 2 random numbers,then check if the answer is right/wrong for 5 times. also, the button would be disabled after clicking 5 times. The other things are clear to me except the idea of how to put it in a loop. can someone help me please? thanks! I tried using the FOR LOOP, but sadly, it would just loop 5 times but it would only check the answer 1 time.I need it to check 5 different answers.
For ctr As Integer = 1 To 5
Button3.Enabled = False
initialize()
If TextBox3.Text = sum Then
MsgBox("correct")
point = point + 1
TextBox3.Focus()
Else
MsgBox("wrong")
MsgBox(sum)
TextBox3.Focus()
End If
Next
MsgBox(point)

If you want to process five different TextBoxes in a loop, one way to do that is to create an array containing all the TextBoxes and loop through the array.
Dim boxes() As TextBox = {TextBox3, TextBoxX, TextBox22, TextBoxBB, TextBoxA}
For Each box As TextBox in boxes
box.Focus()
If box.Text = sum Then
MsgBox.("correct")
point += 1
Else
MsgBox("wrong")
End If
Next
Alternatively, if you want the user to enter five answers one at a time in a single TextBox, you can have the user click a button each time an answer is entered. You should define the counter outside the button's click handler.
Private ctr As Integer
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
ctr += 1
If ctr > 5 Then Button3.Enabled = False
initialize()
If TextBox3.Text = sum Then
MsgBox("correct")
point += 1
Else
MsgBox("wrong")
MsgBox(sum)
End If
MsgBox(point)
End Sub

Related

Trouble with variables, I am making Yahtzee for a school project and can't get some code to work and am still learning how to code

This is my first question in this community and I'd like some help with a school project I am doing.
I am implementing a turn system where every time you select a method to score a variable called turn increases by one so that the appropriate label is changed to the points you got that turn.
This is an example of the Ones option to score which checks all the dice and adds up all the dice which have the value one, when the Ones button is pressed it should update the current turn with the points gained. At the moment it only updates the first turns score. The second and third images are of the changes I tried making which don't work.
[Ones][1]
[Ones changed][2]
[Ones error][3]
```
Private Sub btnOnes_Click(sender As Object, e As EventArgs) Handles btnOnes.Click
Dim Nums(4) As Integer
Nums(0) = RandomNum1
Nums(1) = RandomNum2
Nums(2) = RandomNum3
Nums(3) = RandomNum4
Nums(4) = RandomNum5
For i = 0 To 4
If Nums(i) = 1 Then
OnesScore += 1
End If
Next
lblTurnValue1.Text = OnesScore
Turn += 1
btnOnes.Enabled = False
End Sub
Private Sub btnOnes_Click(sender As Object, e As EventArgs) Handles btnOnes.Click
Dim Nums(4) As Integer
Nums(0) = RandomNum1
Nums(1) = RandomNum2
Nums(2) = RandomNum3
Nums(3) = RandomNum4
Nums(4) = RandomNum5
For i = 0 To 4
If Nums(i) = 1 Then
OnesScore += 1
End If
Next
lblTurnValue1(Turn).Text = OnesScore
Turn += 1
btnOnes.Enabled = False
End Sub
Another problem I have been having is automatically updating labels as I want to make a Newbie mode for my Yahtzee game which displays the numbers beside each option to score so you know what score you are going to get before you choose.
I can't use the Ones button for clicking to represent the score because it's used for scoring the points and whenever I try double click on the label to show its code in Visual Studio it only updates in game when the person clicks the number to show their score which is not what I desire.
[Ones label][4]
Private Sub lblOnes_Click(sender As Object, e As EventArgs) Handles lblOnes.Click
lblOnes.Text = OnesScore
End Sub
In short I want to learn how to use variables within names of objects to allow for the right one to be chosen and how to update labels automatically instead of having to click them. Any resources you can provide to further my understanding of VB.NET is really helpful.
Thanks.
[1]: https://i.stack.imgur.com/zUQM6.png
[2]: https://i.stack.imgur.com/c5nJe.png
[3]: https://i.stack.imgur.com/Y3gUE.png
[4]: https://i.stack.imgur.com/BnwZ8.png

vb.net chnage textbox.text value with for-next loop

I intend to get the value of a Textbox (TNumb.Text) and then use this value (an integer) in other buttons (Private Subs). The number should start at 1 and end at a comboboxvalue (CmbMax.Text) specified by the user. Starting from 1, the program should run through the code lines, perform clicks of the following buttons - (BFirst, BSecond, BFinal). When done with the clicks of these buttons in succession, the TNumb.Text should increase by 1 to 2 and then BFirst, BSecond & BFinal are clicked. Afterward this, another increment of 1 should be done and TNumb should increase again by 1 to 3. This should continue till TMax.Text is reached. All the three buttons should be clicked too. I have used a for-next loop and several other modifications but haven't been successful. Let me also add that in the Load event of the form, I made TNumb.Text=0. The major problem is that when TNumb.text = 1 runs, the value is not increased to 2 for the use and subsequent clicking of the other three buttons. Help will be greatly appreciated. Below is the unsuccessful code I have used:
`
Private Sub Timer1_Tick(sender As Object, e as EventArgs) Handles Timer1.Tick
For j As Integer = 1 to CInt(CmbMax.Text)
TLine.Text = j
Dim h as string = "10:00:00 AM"
Dim g as string = TimeOfDay
Dim w as string = tCpyT.Text
Dim n as string = tTmNow.Text
If h = g or w = n Then
BFirst.PerformClick()
BSecond.PerformClick()
BFinal.PerformClick()
Next
End Sub
`

Selecting an item from a listview and producing a sound file

I have created a listview that has 2 columns, the first column represents English words and the second column represents French words. When i select a row and press a button i want to produce a sound that represents the word selected.
I thought I could identify each row and when a button is pressed the correct sound file will be used, Here is my code so far but it is absolute rubbish... I am open to anything really as long as it will work :)
Private Sub ListenBtn_Click(sender As System.Object, e As System.EventArgs) Handles ListenBtn.Click
Dim x As Integer
x = 0
If ListView1.SelectedItems.Count > 0 Then
ListView1.Items(0) = x + 1
ListView1.Items(1) = x + 2
End If
Select Case x
Case 1 = My.Computer.Audio.Play(My.Resources.hello, AudioPlayMode.WaitToComplete)
Case 2 = My.Computer.Audio.Play(My.Resources.goodbye, AudioPlayMode.WaitToComplete)
End Select
End Sub
There are several ways to approach this, but seeing as how your name is "MassiveNoob", I will try to keep this as simple as possible.
First, Double Click on your button in design view if you are using Visual Studio(which I assume that you are), and in the click event, add the following..
if listview1.selecteditems.count > 0 then
If listview1.SelectedItems(0).Text = "<Whatever is in column 1 of your row>" Then
My.Computer.Audio.Play(My.Resources.hello, AudioPlayMode.WaitToComplete)
ElseIf listview1.SelectedItems(0).Text = "<Whatever is in column 2 of your row>" Then
My.Computer.Audio.Play(My.Resources.goodbye, AudioPlayMode.WaitToComplete)
End If
End If

How to find the highest number in all textboxes

I'm close to finishing a small program that i started, but got stuck with the last part. And I'm just starting to learn programming, so might be a stupid question.
How can i get the text box with the highest number out of 16 boxes,each having its own number, but at the same time keep track of which one still has the highest number every second? Quite confused about the updating it every second part.
All help is appreciated!
Code suggestion
Private Sub findTopTextBox()
Dim topValue As Integer
Dim topTextBox As TextBox
For Each ctrl As Control In Me.Controls 'all the controls on your form
If TypeOf ctrl Is TextBox Then
Dim thisValue As Integer
If Integer.TryParse(DirectCast(ctrl, TextBox).Text, thisValue) Then
If thisValue > topValue Then
topValue = thisValue
topTextBox = DirectCast(ctrl, TextBox)
End If
End If
End If
Next
Debug.Print(String.Concat(topTextBox.Name, " has the top value at: ", topValue))
End Sub
In order to test it each second, you'll need to add a Timer and call this method repeatedly.
You don't really need to check every second only check when a change was made in one of those textboxes
You could handle all your textboxes LostFocus event (using the same method to handle them all) ; get it's text, verify it's a number and if it's greater than the current max update it (along with it's "location" : the textbox control)
That way you always know which one is the greatest
Something along this should do it (typed directly here so not tested) :
Dim maxNumber As Integer, maxTextBox As TextBox
Sub TextBoxes_LostFocus(sender As Object, e As EventArgs) Handles textbox1.LostFocus, textbox2.LostFocus ' ... for all textboxes
Dim tbSender = DirectCast(sender, TextBox)
Dim number As Integer
' Should we update maxTextBox if number = maxNumber ? (if yes change the > to >=)
If Integer.TryParse(tbSender.Text, number) AndAlso number > maxNumber Then
maxNumber = number
maxTexBox = tbSender
End If
End Sub

VB.Net Hangman Game Multiple Rounds

I need some help.
I'm currently creating a Hangman Game in VB.Net.
I'm at the stage where a random word is loaded in from a text file of 6 words, and you can click buttons to guess it. If you guess letters wrong, the frame is shown etc, if they're right, the letter shows in the word through labels.
The next bit, that I'm stuck on, is that multiple rounds are needed. I need there to be 3 turns of this hangman game, so, if you guess the word right you get 10 points, and if you fail, you get 0, And then the game resets with your points and you can play again in Turn 2. Then again in Turn 3, and after Turn 3 finishes, the High Score Form is loaded.
You could create a variable to hold the current round in your Module, at the beginning of each round increase it, and at the end of the round check the current round and make a if then logic.
Dim myRound as Integer = 0
And in the PlayForm in the constructor.
myRound += 1
Once the round is complete.
if myRound >= 3 Then
'open the score page
Else
'start the next round
End if
'In General Declarations:
Dim ButtonList As New List(Of Button) 'or Of Control if you have other types of controls
Dim HgmList As New List(Of PowerPacks.Shape)
Dim AnswerList As New List(Of Label)
'In PlayForm_Load:
With ButtonList
.Clear()
.Add(Me.BtnA)
.Add(Me.BtnB)
.Add(Me.BtnC)
'You get the idea
'Add all your buttons you want to re-enable to the list
End With
With HgmList
.Clear()
.Add(Me.SideFrameLine)
.Add(Me.TopFrameLine)
.Add(Me.CornerFrameLine)
'etc.
End With
With AnswerList
.Add(Me.FirstLetterLbl)
'etc. Just like the other two.
End With
'At the end of your `If Correct = False Then` Block:
Else 'Check for win after each correct guess
Dim Winner As Boolean = True
Dim CheckLetter As Label
For Each CheckLetter in AnswerList
If Not CheckLetter.Visible Then
Winner = False
Exit For
End If
Next
If Winner Then
NextRound(10)
End If
End If
'Somewhere inside your form code:
Private Sub NextRound(RoundScore As Integer)
UserScore += RoundScore
If TurnNumber = 3 Then 'Game Over
MsgBox("Game Over" & vbNewLine & "Score: " & UserScore)
Else 'This is the part you asked about: resetting the form
TurnNumber += 1
PlayForm_Load(Nothing, Nothing)
Dim ResetControl As Control
Dim ResetShape As PowerPacks.Shape
For Each ResetControl In ButtonList
ResetControl.Enabled = True
Next
For Each ResetShape In HgmList
ResetControl.Visible = False
Next
End If
End Sub
I only added the .Clear() to the list builders because you already have your code to get a new word in PlayForm_Load. If you move it (say to a new Sub called NewWord), you don't need .Clear, and you would call your new sub instead of PlayForm_Load.