How to find the highest number in all textboxes - vb.net

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

Related

visual basic: Enter some numbers until a negative value is entered to stop the program

private Sub Command1_Click()
a = InputBox("What is the Number ?", "Input Example"," ")
If a = "-1" Then
End If
End Sub
the whole question is: Enter some numbers until a negative value is entered to stop the program(stop running the what is your number thing). then find the average of the entered numbers.
What I want to do, for now, I want to know how can I make my "a" variable accept any negative value like in the code above it stops when I enter -1 but I want to stop when I enter -3, -10, or any negative value.
There are some helpful answers down below
If you are expecting the usual input to be text then you can use the Double.TryParse method to check if a number was entered. If it was, then you can check if that number is negative, and exit the application if so:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim userMsg As String
userMsg = Microsoft.VisualBasic.InputBox("What is your message?", "Message Entry Form", "Enter your message here", 500, 700)
If userMsg <> "" Then
Dim x As Double
If Double.TryParse(userMsg, x) AndAlso x < 0 Then
Application.Exit()
End If
MessageBox.Show(userMsg)
Else
MessageBox.Show("No Message")
End If
End Sub
The AndAlso operator only looks at the second argument if the first evaluated to True.
If you would like to repeat some portion of code till specific condition is met, you need to use:
While...End While Statement (Visual Basic)
or
Do...Loop Statement (Visual Basic)
It's also possible to write conditional loop using For... Next statement
Dim myNumber As Integer = 0
Dim mySum As Integer = 0
Dim myCounter As Integer = 0
Do
Dim answer As Object = InputBox("Enter integer value", "Getting average of integer values...", "")
'check if user clicked "Cancel" button
If answer <> "" Then
'is it a number?
If Int32.TryParse(answer, myNumber)
If myNumber >=0 Then
mySum += myNumber
myCounter += 1
Else
Exit Do
End If
End If
End If
Loop
Dim average As Double = mySum/myCounter
'you can display average now
Tip: Do not use InputBox, because this "control" is VisualBasic specific. Use custom Form instead. There's tons of examples on Google!

Make a list of all Textbox values in a form

I asked a question before, related to this topic. I wanted to know how to make a loop that adds together all of the textbox.text values in a form. This is the response I got, and it works perfectly.
sum = 0
For Each ctrl In Me.Controls.OfType(Of TextBox)()
Dim txt As TextBox = DirectCast(ctrl, TextBox)
Dim i As Integer = 0
If Integer.TryParse(txt.Text, i) Then
sum = sum + i
End If
Next
What I want to know is how can I, using the same button, get the values of each text box in a form, and put them all together in one list/array?
It's quite a simple problem, but I just can't find the exact syntax for this anywhere.
Also, if a use a similar loop, but do list.Add(text), it just breaks the first loop.
Thanks in advance!
You just need to add the parsed integers to a List(Of Int32):
Dim list As New List(Of Int32)
For Each txt In Me.Controls.OfType(Of TextBox)()
Dim num As Int32 = 0
If Integer.TryParse(txt.Text, num) Then
list.Add(num)
End If
Next
Dim sum = list.Sum() ' easier ;-)
The cast to TextBox is not needed because ctrl is already a TextBox thanks to OfType.
Dim listOfTextBox = Me.Controls.OfType(Of TextBox).ToList()
Dim listOfValue = listOfTextBox.Select(Function(e) Integer.Parse(e.Text))
Dim sum = listOfValue.Sum()

Make richtextbox single line only

Is it possible to make a richtextbox only able to contain one line? I want it to have the wraptext ability but I can't have multiple lines in the file it will generate.
You can set its AcceptsReturn attribute to false that should only allow one line because it won't let the text return.
If for some reason your properties window doesn't have the AcceptsReturn property, you can add if e.KeyCode = Keys.Enter Then e.SuppressKeyPress = True under the event KeyDown (as suggested by Jimi).
Simply think first,how do we get to the next line ? By pressing Return or Enter.So,in the KeyPress event of the RichTextbox you can simply use :
If e.KeyChar = Keys.Return Then
e.Handled = True
End if
Now this has a major drawback and that is : What if a user copy pastes a multi-line text into the richtextbox ?
To fix that, you can simply apply the following code in the TextChanged event :
Private Sub Rtb_TextChanged()
Dim lcount as Integer = rtb.Lines.Count
Dim i As Integer
If lcount > 1 Then
For i = 2 to lcount - 1
Dim index As Integer = rtb.GetFirstCharIndexFromLine(i)
Dim count As Integer = rtb.GetFirstCharIndexFromLine(i + 1) - start_index
rtb.Text = rtb.Text.Remove(index, count)
Next
End if
End Sub
Hope this helps :)

LOOP PROBLEMS in 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

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.