Problems with "running" functionality - vb.net

I am a new programmer learning Visual Basic.
Right now, I'm working on a project about a softball scoreboard. I have been working on this project for a bit, and I am confused on 1 thing.
The thing I am confused on is that I put in a messagebox that said invalid input for negative numbers, but it does not delete it from lstScores and even though the message box appears it still counts as a inning input.
If runs < 0 Then
MessageBox.Show(VALID_MESSAGE)
This is the code:
Public Class frmSoftballScoreboard
Const VALID_MESSAGE As String = "Enter valid runs value"
Const ONLY_MESSAGE As String = "Only seven innings are allowed"
'Declaring array
Dim scores(6) As Double
'declaring variables
Dim runs As String
Dim runningScore As Integer = 0
Dim i As Integer = 0
Dim out As Double
'page load event
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstScores.Items.Add("Runs : Running Score")
End Sub
'Enter score button
Private Sub btnScore_Click(sender As Object, e As EventArgs) Handles btnScore.Click
If i < scores.Length Then
'display inputbox to the user
runs = InputBox("Enter score for " & (i + 1) & " innings", "Score")
'if runs is entered
If runs <> "" Then
'parse the value of runs
If (Double.TryParse(runs, out)) Then
'parse the runs and add it to the array scores()
scores(i) = Double.Parse(runs)
runningScore += scores(i)
'add the rainfall value to the listbox along with month name
lstScores.Items.Add(scores(i) & " :" & runningScore)
'increment the value of i
i = i + 1
Else
'display error message
MessageBox.Show(VALID_MESSAGE)
lblTotal.Text = ""
End If
Else
'if runs is empty then display error message
MessageBox.Show("Enter runs for " & i & "innings")
End If
Else
MessageBox.Show(ONLY_MESSAGE)
End If
If runs < 0 Then
MessageBox.Show(VALID_MESSAGE)
End If
'calculate total runs And display on the lable
If scores(6) = 7 Then
lblTotal.Text = String.Format("final score is {0}", scores.Sum())
End If
End Sub
'Clear Menu click
Private Sub ClearToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles mnuClear.Click
lstScores.Items.Clear()
lblTotal.Text = ""
'reset i to 0
i = 0
End Sub
'Exit Menu click
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles mnuExit.Click
'close application
Application.Exit()
End Sub
End Class
I would really appreciate it if you could help. Thank you.

Private Sub btnScore_Click(sender As Object, e As EventArgs) Handles btnScore.Click
If i < scores.Length Then
'display inputbox to the user
runs = InputBox("Enter score for " & (i + 1) & " innings", "Score")
'if runs is entered
If runs < 0 Then
MessageBox.Show(VALID_MESSAGE)
Exit Sub
ElseIf runs <> "" Then
'parse the value of runs
If (Double.TryParse(runs, out)) Then
'parse the runs and add it to the array scores()
scores(i) = Double.Parse(runs)
runningScore += scores(i)
'add the rainfall value to the listbox along with month name
lstScores.Items.Add(scores(i) & " :" & runningScore)
'increment the value of i
i = i + 1
Else
'display error message
MessageBox.Show(VALID_MESSAGE)
lblTotal.Text = ""
End If
Else
'if runs is empty then display error message
MessageBox.Show("Enter runs for " & i & "innings")
End If
Else
MessageBox.Show(ONLY_MESSAGE)
End If
If runs < 0 Then
MessageBox.Show(VALID_MESSAGE)
End If
'calculate total runs And display on the lable
If scores(6) = 7 Then
lblTotal.Text = String.Format("final score is {0}", scores.Sum())
End If
End Sub
This is the reason why if you input invalid data it will add into lstScores, because your If statement.. is in bottom of your code, although is not recommend where you put the If else statement... Remember reading of the code is start in top to bottom.
Your first If statement is like this. If runs <> "" then ...., of course if you type the -1 value in the Input Text the Boolean will result to true, If -1 <> "" = true, then it will proceed to the statement which is
If (Double.TryParse(runs, out)) Then
'parse the runs and add it to the array scores()
scores(i) = Double.Parse(runs)
runningScore += scores(i)
'add the rainfall value to the listbox along with month name
lstScores.Items.Add(scores(i) & " :" & runningScore)
'increment the value of i
i = i + 1
This is the line of code even the value is invalid or not it still adding value in the lstScores, lstScores.Items.Add(scores(i) & " :" & runningScore)
Now after that statement you will receive a message which is :
Enter valid runs value
If runs < 0 Then
MessageBox.Show(VALID_MESSAGE)
End If
That code is the reason why you receiving the message. If you input -1 of course the result of boolean is true, why? -1 is lessthan to 0 which is true.
The thing i do is, I've insert If statement.. above, which is If runs < 0 then .... and also Exit Sub to instantly end the statement. If you input -1 to Input Text the result is something like this, if runs(-1) lessthan to 0 the Boolean result is true then it will proceed to statement which is the Message and Exit Sub.
Try my code above, and also use Breakpoints.. Hope this helps..

Related

How do I get the programme to allow the user to change info if it is enterred incorrectly during a loop?

I am writing a programme that allows a user to enter personal details but each detail is limited by character length or something like that. I need it to allow the user to enter the details wrong 5 time before being cleared and sent back to a previous form. Im currently trying to use a loop for this but at the moment when you press the enter details button it just loops 5 times straight away and sends the user back without the user being able to change details and try again. Here is what I have so far.
Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
'Declare variables
'This is the form that i will only allow the operator to enter 5 mistakes before being locked out
Dim strStreetAddress As String = txtStreetAddress.Text
Dim strTown As String = txtTown.Text
Dim strCounty As String = txtCounty.Text
Dim strEircode As String = txtEircode.Text
Dim mskPhoneNumber As String = mskNumber.Text
Dim intErrorCount As Integer = 0
'set up if statements
Do Until intErrorCount = 5
If strStreetAddress.Length = 0 Or strTown.Length = 0 Or strCounty.Length = 0 Or mskPhoneNumber.Length = 0 Or strEircode.Length = 0 Then
intErrorCount += 1
MessageBox.Show("One or more details have not been enterred")
txtStreetAddress.Clear()
txtCounty.Clear()
txtEircode.Clear()
txtTown.Clear()
mskNumber.Clear()
ElseIf strStreetAddress.Length > 50 Or strTown.Length > 15 Or strCounty.Length > 10 Or strEircode.Length <> 7 Or mskPhoneNumber.Length <> 10 Then
intErrorCount += 1
MessageBox.Show("One or more detais has been entered incorrectly")
txtStreetAddress.Clear()
txtCounty.Clear()
txtEircode.Clear()
txtTown.Clear()
mskNumber.Clear()
Else MessageBox.Show("All Details Entered Correctly")
Me.Hide()
frmPurchaseScreen.Show()
End If
Loop
'if errorcount reaches 5 then operator must login again and restart
If intErrorCount = 5 Then
MessageBox.Show("You have made too many errors, you must Login again")
Me.Hide()
frmLogin.Show()
End If
End Sub
Private Sub txtErrorCount_TextChanged(sender As Object, e As EventArgs) Handles txtErrorCount.TextChanged
End Sub
End Class
It's because you declare your error counter inside the sub and because the user can't change his input when you loop.
If you declare a variable in a sub, it will disappear as soon as the code exits the sub. To keep information outside of a sub, you need to declare the variable at a higher level.
Don't loop inside the sub called when the user click and remember the errors number with a higher level variable :
Private intErrorCount As Integer = 0
Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Declare variables
'This is the form that i will only allow the operator to enter 5 mistakes before being locked out
Dim strStreetAddress As String = txtStreetAddress.Text
Dim strTown As String = txtTown.Text
Dim strCounty As String = txtCounty.Text
Dim strEircode As String = txtEircode.Text
Dim mskPhoneNumber As String = mskNumber.Text
If strStreetAddress.Length = 0 Or strTown.Length = 0 Or strCounty.Length = 0 Or mskPhoneNumber.Length = 0 Or strEircode.Length = 0 Then
intErrorCount += 1
MessageBox.Show("One or more details have not been enterred")
txtStreetAddress.Clear()
txtCounty.Clear()
txtEircode.Clear()
txtTown.Clear()
mskNumber.Clear()
ElseIf strStreetAddress.Length > 50 Or strTown.Length > 15 Or strCounty.Length > 10 Or strEircode.Length <> 7 Or mskPhoneNumber.Length <> 10 Then
intErrorCount += 1
MessageBox.Show("One or more detais has been entered incorrectly")
txtStreetAddress.Clear()
txtCounty.Clear()
txtEircode.Clear()
txtTown.Clear()
mskNumber.Clear()
Else MessageBox.Show("All Details Entered Correctly")
Me.Hide()
frmPurchaseScreen.Show()
End If
'if errorcount reaches 5 then operator must login again and restart
If intErrorCount = 5 Then
MessageBox.Show("You have made too many errors, you must Login again")
Me.Hide()
frmLogin.Show()
End If
End Sub

How to debug a cast execption in vb.net?

I am getting a cast exception and I have re-written this code a large number of times. I am getting the exception on the following line:
If (CInt(hHurricaneYear) < CInt(_strYears(hAverage))) Then
And I am only getting results in the lblNumberOfHurricans. the other two labels are not showing any results. I thought I was getting it when the cast exception showed up.
Can anyone suggest how to get the results and stop the exception?
Here is what I have so far (well at least the last try).
Option Strict On
Public Class frmHurricaneStatistics
' Class level Private variables.
Public Shared _intSizeOfArray As Integer = 20
Private _strYears(_intSizeOfArray) As String
Private _intNumberOfHurricans(_intSizeOfArray) As Integer
Private Sub frmHurricaneStatistics_Load(sender As Object, e As EventArgs
) Handles MyBase.Load
' This load event reads the inventory text file and fills
' the ComboBox object with the Hurricane Statistics.
' Initialize an instace of the streamreader object and declare variables.
Dim objReader As IO.StreamReader
Dim strHurricaneStatistics As String = "Hurricanes.txt"
Dim intCount As Integer = 0
Dim intFill As Integer
Dim strFileError As String = "The file is not available. Please restart the
application when the file is available."
' Verify the Hurricane.txt file exists.
If IO.File.Exists(strHurricaneStatistics) Then
objReader = IO.File.OpenText(strHurricaneStatistics)
' Read the file line by line until the file is completed.
Do While objReader.Peek <> -1
_strYears(intCount) = objReader.ReadLine()
_intNumberOfHurricans(intCount) = Convert.ToInt32(objReader.ReadLine())
intCount += 1
Loop
objReader.Close()
' The ComboBox objext is filled with the Years for Hurricanes.
For intFill = 0 To (_strYears.Length - 1)
cmbYears.Items.Add(_strYears(intFill))
Next
Else
MsgBox(strFileError, , "Error")
Close()
' If ComboBox is filled then enable the Display Statistics button.
'btnDisplayStatistics.Enabled = True
End If
End Sub
Private Sub btnDisplayStatistics_Click(sender As Object, e As EventArgs
) Handles btnDisplayStatistics.Click
' This click event calls the sub procedures for the selected years and
' the number of hurricans in that year.
Dim intSelectedYear As Integer
Dim strMissingSelection As String = "Missing Selection"
Dim strSelectAYearError As String = "Please Select a Year"
' If the ComboBox object has a selection, Display Statistics.
If cmbYears.SelectedIndex >= 0 Then
intSelectedYear = cmbYears.SelectedIndex
Else
MsgBox(strSelectAYearError, , strMissingSelection)
End If
Private Sub btnDisplayStatistics_Click(sender As Object, e As EventArgs
) Handles btnDisplayStatistics.Click
' This click event calls the sub procedures for the selected years and
' the number of hurricans in that year.
Dim intSelectedYear As Integer
Dim strMissingSelection As String = "Missing Selection"
Dim strSelectAYearError As String = "Please Select a Year"
' If the ComboBox object has a selection, call the Display Statistics procedure.
If cmbYears.SelectedIndex >= 0 Then
intSelectedYear = cmbYears.SelectedIndex
Else
MsgBox(strSelectAYearError, , strMissingSelection)
End If
' This procedure MakeLabelsVisible Is called to display the labels
' And the results.
MakeLabelsVisible()
Dim hHurricaneAverage As Integer
Dim hHurricaneYear As Integer = 0
For hAverage As Integer = 0 To _strYears.Length - 1
If (CInt(hHurricaneYear) < CInt(_strYears(hAverage))) Then
hHurricaneYear = CInt(CType(CInt(_strYears(hAverage)), String))
End If
hHurricaneAverage = hHurricaneAverage + CInt((_strYears.ToString))
hHurricaneAverage = CInt(hHurricaneAverage / _strYears.Length)
Next
' Display the statistics for the Storm Average in the selected Year
' and the most active year within the range of year.
lblNumberOfHurricanes.Text = "The Number of Hurricanes in the Year " &
_strYears(intSelectedYear) & " is " & _intNumberOfHurricans(intSelectedYear).ToString() & "."
lblAvergeNumberHurricanes.Text = "The Average Number of Storms was " &
hHurricaneAverage & " Hurricanes."
Dim intSizeOfArray As Integer = Nothing
lblMostStorms.Text = "The Year "(CInt(_strYears(CInt(hHurricaneYear.ToString())) & "
Had The Most Storms Between " & (_strYears(0) & _strYears(20).ToString)))
End Sub
Option strict on
Your error lies in that you are trying to convert an entire string array into an integer:
hHurricaneAverage = hHurricaneAverage + CInt((_strYears.ToString))
You will need to call the index of _strYears:
hHurricaneAverage = hHurricaneAverage + CInt((_strYears(hAverage).ToString))
This will also explain why the other labels do not update, because hHurricanAverage never gets calculated.

How to show all validation errors as a list in my messagebox

I am having a problem with my code. I am trying to show all the validation errors in a message box. Can anyone tell me why only one of my errors is showing up in the box? I tried a couple more solutions and looked around but I need a little help please.
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
If Data_Validated_ok() = False Then Exit Sub
End Sub
Private Function Data_Validated_ok() As Boolean
Dim intErrCount As Integer
Dim strErrMessage As String = String.Empty
Dim ctrlError As New Collection
' make sure Premium channel is selected
If Me.lstPremium.SelectedIndex < 0 Then
intErrCount = intErrCount + 1
strErrMessage = intErrCount & ". Premium Channels is a required field." _
& vbCrLf
ctrlError.Add(lstPremium.SelectedIndex)
End If
' make sure a customer type is selected in the Radioboxes
If radBusiness.Checked = False And
radResidential.Checked = False Then
intErrCount = intErrCount + 1
strErrMessage = intErrCount & ".Customer Type is a required field." _
& vbCrLf
ctrlError.Add(radBusiness.Checked, radResidential.Checked)
End If
' make sure a business customer checks at least one option in the listbox
If radBusiness.Checked = True And Me.lstConnections.SelectedIndex < 0 Then
intErrCount = intErrCount + 1
strErrMessage = intErrCount & ". Business Customers must select 1 or more Connection." _
& vbCrLf
ctrlError.Add(lstConnections.SelectedIndex)
End If
' show all errors in a messagebox
If intErrCount > 0 Then
MessageBox.Show(strErrMessage, "Validation Rule(s)", MessageBoxButtons.OK, MessageBoxIcon.Information)
Dim ctrl As Control
ctrl = ctrlError.Item(1)
ctrl.Focus()
Return False
Else
Return True
End If
End Function
How about storing each error in a List(Of String)? Your variable ctrlError is not storing controls, but integers and booleans - you should have casting errors there.
Private Function Data_Validated_ok() As Boolean
Dim errorMsgs As New List(Of String)
' make sure Premium channel is selected
If Me.lstPremium.SelectedIndex < 0 Then
errorMsgs.Add("Premium Channels is a required field.")
End If
' make sure a customer type is selected in the Radioboxes
If radBusiness.Checked = False AndAlso
radResidential.Checked = False Then
errorMsgs.Add("Customer Type is a required field.")
End If
' make sure a business customer checks at least one option in the listbox
If radBusiness.Checked = True And Me.lstConnections.SelectedIndex < 0 Then
errorMsgs.Add("Business Customers must select 1 or more Connection.")
End If
' show all errors in a messagebox
If errorMsgs.Count > 0 Then
MessageBox.Show(String.Join(Environment.Newline, errorMsgs.ToArray), "Validation Rule(s)", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return False
Else
Return True
End If
End Function

lockers array program VISUAL BASIC

I have to do a program in Visual Basic that displays the status of 100 lockers being either open or closed using a Boolean array. When the button Initialize is clicked, all the lockers should have a status of opened, but when Simulate is clicked, it goes through a process of closing every Nth locker (every 2nd locker, then every 3rd locker, then every 4th locker, and so on).
I have it working so that it always displays opened for every locker, but I can't figure out how to make it close every Nth locker.
Here is my code:
Public Class Form1
Dim index As Integer
Dim doors(100) As Boolean
Private Sub btnInitialize_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInitialize.Click
Dim count As Integer
lstLockers.Items.Clear()
lstLockers.Items.Add("Locker" & vbTab & "Status")
For count = 1 To 100
doors(count) = True
If doors(count) = True Then
lstLockers.Items.Add(count & vbTab & "Opened")
End If
Next
End Sub
Private Sub btnSimulate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSimulate.Click
lstLockers.Items.Clear()
lstLockers.Items.Add("Locker" & vbTab & "Status")
Dim count As Integer
Dim eq As Integer
For count = 1 To 100
doors(count) = True
If doors(count) = True Then
lstLockers.Items.Add(count & vbTab & "Opened")
ElseIf doors(count) = False Then
lstLockers.Items.Add(count & vbTab & "Closed")
End If
Next
End Sub
End Class
In your btnSimulate_Click method, you are setting doors index to true and then immediately checking if it is true or not. This is why it always says opened.
Regarding closing every Nth locker, you can do this with a simple counter variable.
' n represents the "Nth" locker to close.
Dim n As Integer = 2
Dim progress As Integer = 0 ' Progress to n.
For count = 1 To 100
' This line shouldn't be here. doors has already been initialized.
'doors(count) = True
' Increment progress towards n.
progress = progress + 1
' Check if Nth interval is reached.
If n = progress Then
' It is. Close the locker.
doors(count) = False
' Increment n and reset the progress counter.
n = n + 1
progress = 0
End If
If doors(count) = True Then
lstLockers.Items.Add(count & vbTab & "Opened")
ElseIf doors(count) = False Then
lstLockers.Items.Add(count & vbTab & "Closed")
End If
Next
This would close lockers:
2 (n = 2)
5 (n = 3)
9 (n = 4)
14 (n = 5)
etc.

Index out-of-range error

I am getting an error when I execute this button event: here is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Try
' get the details of the item
Dim R As Your_pharmacy.POSDS.ItemsRow = Button1.Tag
' next search for the barcode in the datagridview
Dim I As Integer
Dim ItemLoc As Integer = -1
For I = 0 To DGV1.Rows.Count - 1
If R.barcodeNumber = DGV1.Rows(I).Cells(0).Value Then
' item found
ItemLoc = I
Exit For
End If
Next
' if item is not found, add it
If ItemLoc = -1 Then
DGV1.Rows.Add(R.barcodeNumber, R.ItemName, R.BuyPrice, R.SellPrice, 1, R.SellPrice)
Else
' if item is already there increase its count
Dim ItemCount As Long = DGV1.Rows(ItemLoc).Cells(4).Value
ItemCount += 1
Dim NewPrice As Decimal = R.SellPrice * ItemCount
DGV1.Rows(ItemLoc).Cells(4).Value = ItemCount
DGV1.Rows(ItemLoc).Cells(5).Value = NewPrice
End If
' next clear textbox1 and set focus to it
TextBox1.Text = ""
TextBox1.Focus()
' compute the total for the recipt
Dim Sum As Decimal = 1
For I = 0 To DGV1.Rows.Count - 1
Sum += DGV1.Rows(I).Cells(5).Value 'here the error happens
Next
TextBox4.Text = Sum
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Error")
End Try
End Sub
The error details:
error: index was out of range.must be non-negative and less than the
size of the collection. parameter name: index vb.net
DGV1 must have fewer cells than 5. When the error occurs, use a breakpoint and the debug watch window to see how many cells there are in DVG1(I). Maybe the first one is created with zero cells?