How to exit loop with "keyword" - vb.net

Here's my code:
Dim n As String, upper As String, lower As String
Label2.Text = "EXIT"
Label3.Text = "exit"
upper = Label2.Text
Label2.Text = upper.ToUpper
lower = Label3.Text
Label3.Text = lower.ToLower
Do
Label1.Text = InputBox(" Enter a word. If you would like to quit, type: EXIT in the inputbox")
n = Label1.Text
Label1.Text = n.Length
Label1.Text = Label1.Text & " letters"
Loop Until n = ""
Label1.Visible = False
MsgBox(" Invalid value!")
End
Do
Label1.Text = InputBox(" Enter a word. If you would like to quit, type: EXIT in the inputbox")
n = Label1.Text
Label1.Text = n.Length
Label1.Text = Label1.Text & " letters"
Loop Until upper = Label2.Text
Label1.Visible = False
MsgBox(" You have exited the program!")
End
Do
Label1.Text = InputBox(" Enter a word. If you would like to quit, type: EXIT in the inputbox")
n = Label1.Text
Label1.Text = n.Length
Label1.Text = Label1.Text & " letters"
Loop Until lower = Label3.Text
Label1.Visible = False
MsgBox(" You have exited the program!")
End
The issue I'm having with this program is when I run it and press the cancel button or the X button to exit, it executes correctly with the loop. But with the other 2 loops, when I type in "exit" or "EXIT" in uppercase or lowercase, it just displays how many characters are in the word instead of ending the program.

While the answer is missing, I state that the String "Label3" is not part of the InputBox String defaultValue. The "Label1" Text is used and not changed to be saved to "Label2" or "Label3".
The Do Statement seems to iterate with integer datatypes. An sample is to be found on a page for do loop statements.
The negotiation for retriving examples leads to choose either a character or a number. And until a boolean statement is not recognized redirect text like above
Loop Until n = ""
is the easy part.
A newline for an integer value is not necessary a boolean value. The integer can be a boolean value. If integers are dissected until they can be used for a boolean logic the possibiltiy can be used for iterations.
So a string like Text "Label1" is not an obvious boolean value.
I can not sample the question with an answer that takes the whole process apart, but post a part that exits the loop linking the string to a boolean value.
Public Class Form1
Private myValue As Object
ReadOnly AntiPanel As New Label
Private boolenValueTools As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim message, title, defaultValue As String
' Set prompt.
message = " Enter a word. If you would like to quit, type: EXIT in the inputbox"
' Set title.
title = "InputBox Demo"
defaultValue = "1" ' Set default value.
'' Display dialog box at position 100, 100.
'myValue = InputBox(message, title, defaultValue, 100, 100)
'' If user has clicked Cancel, set myValue to defaultValue
'If myValue Is "" Then myValue = defaultValue
' Display message, title, and default value.
Do
myValue = InputBox(message, title, defaultValue)
If myValue = "EXIT" Then boolenValueTools = True
Loop Until boolenValueTools = True
' If user has clicked Cancel, set myValue to defaultValue
If myValue Is "" Then myValue = defaultValue
Me.Controls.Add(AntiPanel)
With AntiPanel
.AutoSize = True
.BackColor = Color.Transparent
.Location = New Point(100, 100)
.Font = New Font("Consolas", 55.75, FontStyle.Regular)
.Text = myValue 'in my case also known as NotNamedInputBoxTextString
End With
End Sub
End Class

Related

How do I make my calculator accept multiple values and multiple operators before pressing the equal button?

I'm a newbie in VB.NET so I followed a tutorial. The current code only takes the first value, operator, and then the second value before I press the equal button. It's working as intended. But I want to know how to make it accept multiple values and multiple operators before I press the Equal button.
Here is the current code for the operator buttons and the equal button and the variables declared:
Dim firstval, secondval, result As Double
Dim op As String
Private Sub Operator_Click(sender As Object, e As EventArgs) Handles Button17.Click, Button16.Click, Button15.Click, Button14.Click
Dim b As Button = sender
firstval = Label1.Text
Label2.Text = Label1.Text
Label1.Text = ""
op = b.Text
Label2.Text = Label2.Text + " " + op
End Sub
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
secondval = Label1.Text
If op = "+" Then
result = firstval + secondval
Label1.Text = result
ElseIf op = "-" Then
result = firstval - secondval
Label1.Text = result
ElseIf op = "×" Then
result = firstval * secondval
Label1.Text = result
ElseIf op = "÷" Then
result = firstval / secondval
Label1.Text = result
If secondval = "0" Then
Label1.Text = ""
Label3.Text = "CANNOT DIVIDE BY ZERO"
End If
End If
End Sub
You could use a string to store the entire math problem the user inputs and add the number or operator to it depending on the button you click. This way you could have practically infinite amounts operators and numbers.
When the string is done you can let it calculate when the "=" button or whatever button you're using is pressed.
You would start by declaring the variable
Dim input As String
Then use this code when you click a button
input = input + NAMEOFBUTTON.text
This will make it add the content of the button to the current input. NAMEOFBUTTON would be the name of the button the code runs under.
You might need to use "/" as the button name for division.
When clicking on "=" this code should run:
Dim output = New DataTable().Compute(input, Nothing)
Then you could use a label to display the output. NAMEOFLABEL is of course the name of the label you display it on.
You might need to convert the output to a string before showing it on a label.
NAMEOFLABEL.text = output.ToString

Is there something wrong with my coding? I am still new

I try to run this program but an error always appears:
Conversion from the string "LBLBuku" to type' Double 'is not valid.
If LBLBuku.Text >= 5 Or Val(LBLBuku.Text) + Val(TextBox1.Text) > 5 Then
MsgBox("Peminjaman Melebihi")
Else
If lbljudul.Text = "" Or TextBox1.Text = "" Then
MsgBox("Silahkan isi Kode Buku")
Else
DataGridView1.Rows.Add(New String() {TextBox2.Text, lbljudul.Text, LBLPengarang.Text, LBLTahun.Text, TextBox2.Text})
TextBox1.Text = ""
TextBox2.Text = ""
lbljudul.Text = ""
TextBox2.Text = ""
LBLPengarang.Text = ""
LBLTahun.Text = ""
Call rumustotalbuku()
End If
End If
Notice on your code the line
LBLBuku.Text >= 5
The property Text is of type String, you would have to convert the text to an integer type first before you can use ">=".
First I declare a variable to hold the integer value in the Text Box. Integer.TryParse will return true if it can convert the string in the text box to an integer. It will also fill the variable intTB1 with the number.
I am assuming that LBLBuku is a label so the .Text property has been set from code. We can depend on this being a number so all we need to do is the conversion with CInt(). We can use the variable we got from the .TryParse in the Or CInt(LBLBuku.Text) + intTB1 > 5 instead of referring to the text box again.
We don't need to check if TextBox1 is empty because it wouldn't have passed the .TryParse if it was.
Last and probably least, you don't need the Call keyword in most situations.
You do realize that you have added TextBox2 twice to the new DataRow.
Private Sub OPCode()
Dim intTB1 As Integer
If Not Integer.TryParse(TextBox1.Text, intTB1) Then
MessageBox.Show("Please enter a number in TextBox1.")
Return
End If
If CInt(LBLBuku.Text) >= 5 Or CInt(LBLBuku.Text) + intTB1 > 5 Then
MsgBox("Peminjaman Melebihi Maksimal")
Else
If lbljudul.Text = "" Then
MsgBox("Silahkan isi Kode Buku")
Else
DataGridView1.Rows.Add(New String() {TextBox2.Text, lbljudul.Text, LBLPengarang.Text, LBLTahun.Text, TextBox2.Text})
TextBox1.Text = ""
TextBox2.Text = ""
bljudul.Text = ""
TextBox2.Text = ""
LBLPengarang.Text = ""
LBLTahun.Text = ""
rumustotalbuku()
End If
End If
End Sub

How to stop message box repeating because of array?

I'm trying to make a hangman game and to input "incorrect" if the user picks the wrong letter but it will keep repeating because of the randomWord which chooses the word from an array how do I stop this?
Sub wordGeneration()
Dim wordUsed As Array = {"pizza", "noodle", "zombie", "object", "rowin", "running", "elephant", "lion"}
Dim random As New Random
randomWord = wordUsed(random.Next(0, 8))
Label2.Text = randomWord
End Sub
Sub letterInput()
For i As Integer = 0 To randomWord.Length - 1
If userInput = randomWord(i) Then
MessageBox.Show("correct")
ElseIf userInput <> randomWord(i) Then
MessageBox.Show("incorrect")
Label4.Text = counter
End If
Next
End Sub
Simpler and faster to let VB do the search for you.
Sub letterInput()
if InStr(randomWord.Length, userInput) > 0
MessageBox.Show("correct")
Else
MessageBox.Show("incorrect")
Label4.Text = counter
End If
End Sub
Note: I am assuming userInput is a single character.

Null Reference Exception was Unhandled

This is a very simple code. I am just beginning to use vb. Im making a game where you reorder the shuffled numbers into the correct order. You try to solve the puzzle in the least amount of time and least amount of clicks, and the next time you play, you try to get a lower score (or beat that record). You have sixteen buttons (4x4) and fifteen numbers. There is a checkbutton function to see if the puzzle is solved Every time I debug the program, it highlights the Clicks and FinalTime and says Null Reference Exception was Unhandled. Heres some of the code.
Public Class Form1
Dim Clicks As Integer = 0 'The variable that counts the number of times you clicked
Dim Time As Integer 'The vairable that holds the time
Dim TimeMin As Integer 'The variable that holds the minutes
Dim TimeSec As Integer 'The variable that holds the seconds
Dim FinalTime As String 'The variable that holds the final time (minutes and seconds)
Dim lngArray() As String = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", ""} {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", ""}
Dim NumArray() As Integer
Dim NumArray1() As String
Private Sub CheckButton(ByRef Butt1 As Button, ByRef Butt2 As Button)
If Butt2.Text = "" Then 'Checks if the button around the clicked number is empty "" It's the location of the empty square that you want the number in Butt1 to move to.
Butt2.Text = Butt1.Text 'If it is, the value from Butt2 will "copy and paste" into Butt1
Butt1.Text = "" 'Butt1 will keep the value and the value in Butt2 will clear ""
Clicks += 1 'Incrementing Each click by only 1. It's a counter. In addition, the code above only allows the click to increment by 1 if the number has moved. If you just click on a number that can't move, the click would not count.
End If
If Clicks > 1 Then
lblTotal.Text = Clicks & " Clicks" 'Shows the total amount of clicks when it is greater than one
Else
lblTotal.Text = Clicks & " Click" 'Shows the total amount of clicks when it is one
End If
End Sub
Private Sub CheckSolved() 'A private sub that checks if the puzzle is solved ,restarts the count, and stops the time
Dim strName As String = "" 'The variable that has the string of the winners name
If Me.Button1.Text = "1" And Me.Button2.Text = "2" And Me.Button3.Text = "3" And Me.Button4.Text = "4" And Me.Button5.Text = "5" And Me.Button6.Text = "6" And Me.Button7.Text = "7" And Me.Button8.Text = "8" And Me.Button9.Text = "9" And Me.Button10.Text = "10" And Me.Button11.Text = "11" And Me.Button12.Text = "12" And Me.Button13.Text = "13" And Me.Button14.Text = "14" And Me.Button15.Text = "15" Then 'Checks if the numbers are in the correct buttons
Timer1.Enabled = False 'Stops the time
strName = InputBox("What is your name", "Name of winner") 'Get's the winners name
MessageBox.Show("In " & FinalTime & " , you solved the puzzle in " & Me.Clicks & " clicks! Congratulations " & strName) 'Messagebox showing how many times you clicked to solve the puzzle. It gets the name you typed into the inputbox (strname) and displays it
Call Restart() 'Shuffles the buttons and restarts the game when you win
Call Record(NumArray, NumArray1)
End If
End Sub
Private Sub Record(ByVal NumArray() As Integer, ByVal NumArray1() As String)
'Make timemin array and then make time array. Then make click array
For i As Integer = 0 To 1000
NumArray(i) = Clicks 'This is where the Null Reference error occured
i = +1
Array.Sort(NumArray) 'sorting the array values from least to greatest
Next i
lblRecordClicks.Text = NumArray(0) & " Clicks" 'displaying the lowest number of clicks in the label
For k As Integer = 0 To 1000 'Making an integer that captures 1000 values
NumArray1(k) = FinalTime 'This is where the Null Reference error occured
k = +1
Array.Sort(NumArray1) 'sorting the array values from least to greatest
Next k
lblRecordTime.Text = NumArray1(0) 'displaying the lowest time in the label
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Call Times()
End Sub
Private Sub Shuffle(ByVal lngArray As Object) 'Shuffles the values of the Array
Dim i As Long
Dim iMin As Long
Dim iMax As Long
Dim lngReplace As Long
Dim varSwap As Object
iMin = LBound(lngArray)
iMax = UBound(lngArray)
For i = iMax To iMin + 1 Step -1
lngReplace = Int((i - iMin + 1) * Rnd() + iMin)
varSwap = lngArray(i)
lngArray(i) = lngArray(lngReplace)
lngArray(lngReplace) = varSwap
Next
Button1.Text = lngArray(0)
Button2.Text = lngArray(1)
Button3.Text = lngArray(2)
Button4.Text = lngArray(3)
Button5.Text = lngArray(4)
Button6.Text = lngArray(5)
Button7.Text = lngArray(6)
Button8.Text = lngArray(7)
Button9.Text = lngArray(8)
Button10.Text = lngArray(9)
Button11.Text = lngArray(10)
Button12.Text = lngArray(11)
Button13.Text = lngArray(12)
Button14.Text = lngArray(13)
Button15.Text = lngArray(14)
Button16.Text = lngArray(15)
End Sub
Private Sub RestartToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RestartToolStripMenuItem.Click
Call Restart()
Call Me.btnStart_Click(sender, e) 'Call the button click to enable all buttons
End Sub
Private Sub Times()
Time = Time + 1 'Increase the time by 1 second every interval
TimeSec = TimeSec + 1 'Increase the time by 1 second every interval
TimeMin = Convert.ToInt32(TimeSec \ 60) 'Takes the whole number of the amount of seconds divided by 60 and leaves out the remainder
If Time >= 60 Then
Time = 0
End If
'If the seconds pass 59 (and they do), it restarts to 0
FinalTime = TimeMin & " min " & Time & " seconds" 'Final time is the string displayed showing the final time
lblTime.Text = FinalTime 'The label displays the final time
End Sub
Private Sub Restart()
Time = 0 'Resets the time
Clicks = 0 'Resets the amount of clicks
lblTotal.Text = "" 'Clears the label
lblCheat.Visible = False
lblTotal.Visible = True
lblClicks.Visible = True
lblQuote.Visible = True
lblRecordClicks.Visible = True
lblRecordTime.Visible = True
'If the user cheated and hit the solve button and wants to restart and solve the puzzle on their own, then he can. Turning these label settings to true allows you to see them again
Call Shuffle(lngArray) 'Shuffles the numbers
Timer1.Enabled = True 'Continues the time when it resets to 0
End Sub
Public Sub RefreshEverythingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshEverythingToolStripMenuItem.Click
Timer1.Enabled = False 'Pauses the timer
If MessageBox.Show("Do you want to restart everything?", "Restart Game", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
Time = 0
TimeSec = 0
'Resets the time
Clicks = 0 'Resets the amount of clicks
lblTotal.Text = "" 'Clears the label
lblRecordClicks.Text = "" 'Resets the record click
lblRecordTime.Text = "" 'Resets the record time
End If
Call Shuffle(lngArray) 'Shuffles the numbers
Timer1.Enabled = True 'Continues the timer if no is selected
ReDim NumArray(1000) 'Clears the array data that was captured
ReDim NumArray1(1000) 'Clears the array data that was captured
End Sub
End Class
If you do decide to help (and I HOPE you do) could you try to avoid using "computer" talk and try to "dumb it down for me". Like I said before, I am new at this. Thanks for your time.
You are trying to save the values to an array. When you define an array like Dim NumArray() As Integer there are no bounds set for the array (the array is basically Nothing) and if you then try to access an array item you encounter the Null reference exception.
You only set the array bounds in Public Sub RefreshEverythingToolStripMenuItem_Click. Now I don't know all of your code but if the game is started without the user specifically clicking this button then your array will be Nothing.
You have to make sure that the array bounds are always set before a game is started.
You could also look into more abstract array-like types in .NET. For example you could try a list. A List is practically an array with arbitrary bounds. You define the List with a specific type using the Of keyword and you can then add items to it with the lists Add method, remove items, sort the list etc.
Define as a global variable:
Dim FinalTimes as New List(Of Integer)
Dim FinalClicks as New List(Of Integer)
Make sure you use the New keyword here. This means that a new object of class List will be created. Otherwise you would encounter Null Reference Exceptions on the lines below again.
When the user solves the puzzle you then do:
FinalTimes.Add(FinalTime)
FinalClicks.Add(Clicks)
FinalTimes.Sort
FinalClicks.Sort
This will basically increase the length of the list by one, write the FinalTime/Clicks value into the last item and then resort both lists. Ideas: For a highscore you might want to connect FinalTime, Clicks and Player Name into a structure and use the IComparer Interface to let the user sort the high score list by name/Time/Clicks.
Jens
PS: Before I forget: The For loops in which the exception occured in your code make no real sense. First problem is that in their current forms they would be infinite loops because you always set i to 1 in the loop while the loop ends when i=1000 (which would never happen, obviously). I think you meant i=i+1 or shorter i += 1 there. But they still make little sense. I tried it out to see what happens exactly (which is not obvious :-)) and half of your high score is overwritten by the new value etc. I think the above method will be easier. :-)

Infinite loop when validating input against array

The homework task is to simply allow the user to input a value (string) "FD__" and match it against a list of known inputs and return true/false. The ID's are already defined and it works well, when I type an ID which is defined like Products(2) which is "FD3" it will return true, if the value is not defined it will not only give no results but crash the program, so in conclusion true works but false does not. Any information could be helpful.
Design: http://i.imgur.com/bJnFAMX.png
Public Class Form1
'variables
Dim Products(9) As String
Dim Entered As String
Dim Found As Boolean
Public Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
'Product variable array elements
Products(0) = "FD1"
Products(1) = "FD2"
Products(2) = "FD3"
Products(3) = "FD4"
Products(4) = "FD5"
Products(5) = "FD6"
Products(6) = "FD7"
Products(7) = "FD8"
Products(8) = "FD9"
Products(9) = "FD10"
'process
Entered = txtFind.Text 'define entered value as variable
Found = FindNumber() 'sub function
If Found Then
lblResult.Text = Found 'change the results
End If
End Sub
Public Function FindNumber()
'If true statements
Do While (Found = False)
If Entered = Products(0) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(1) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(2) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(3) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(4) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(5) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(6) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(7) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(8) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(9) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
Else 'keeps crashing
Found = False
lblResult.ForeColor = Color.Red
End If
Loop
Return Found
End Function
End Class
You can actually do this with a lot less code... I'm going to go ahead and do some critique while at it.
'1. Functions should have return types
'2. You should pass the function what it needs to do its job
Public Function FindNumber(numberEntered As String) As Boolean
'Rather than evaluating if a condition is true or false, and then returning
'true or false, you can just directly return the condition.
Return Products.Contains(numberEntered)
End Function
Then in your main program:
'This doesn't need to be a class level variable
Dim entered = txtFind.Text
'Local variables should be lowercase
Dim found = FindNumber(entered)
If found Then
'With Option Strict you can't assign a Boolean to a String...
'but you can do .ToString() instead
lblResult.Text = found.ToString()
'And this was really dependent on the result of the function...
'Also it has nothing to do with finding the number...
lblResult.ForeColor = Color.LawnGreen
Else
lblResult.ForeColor = Color.Red
End If
It looks a lot longer, but take out the comments and I promise you it'll be a much simpler (and shorter) program.
You could optimise this whole code by using the .contains function this simple used like this:
if mylist.contains("sometext") then
'change forecolor to green
else
'change forecolor to red
end if
This would mitigate your problem.