Error Message TryParse - vb.net

I am struggling with my TryParse method. The program is to calculate the total rainfall and average rainfall. User enters each month in inches. The totals are accurate, but the error message "enter numeric value" pops up when you click calculate. The program also calculates with a symbol or letter, how would i go about stopping it at that point.
Public Class Korszun
Private Sub Korszun_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Please enter each month's rainfall average (ex: 1.11 inches)")
End Sub
Private Sub txtJuly_TextChanged(sender As Object, e As EventArgs) Handles txtJuly.TextChanged
End Sub
Private Sub txtOctober_TextChanged(sender As Object, e As EventArgs) Handles txtOctober.TextChanged
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'declare the variables
Dim intCount As Integer = 0
Dim dblAverageRainfall As Double
Dim dblTotalRainfall As Double
Dim dblInches As Double
'create txtbox variable
'go through each TextBox control in the Groupbox and add them all up
For Each txtBox As TextBox In Me.GroupBox1.Controls.OfType(Of TextBox)()
If Not Double.TryParse(txtBox.Text, dblInches) Then
MessageBox.Show("Please enter numerical values only.")
ElseIf CDbl(txtBox.Text) < 0 Then
MessageBox.Show("Please enter all positive numbers.")
ElseIf TypeOf txtBox Is TextBox Then
'add the total value of rainfall and put it in the decTotalRainfall variable
dblTotalRainfall += CDbl(txtBox.Text)
End If
Next
'get the average
dblAverageRainfall = dblTotalRainfall / 12
'show the results in the labels
lblTotal.Text = dblTotalRainfall.ToString("n2")
lblAverage.Text = dblAverageRainfall.ToString("n5")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'Clear the Textboxes
txtJanuary.Clear()
txtFebruary.Clear()
txtMarch.Clear()
txtApril.Clear()
txtMay.Clear()
txtJune.Clear()
txtJuly.Clear()
txtAugust.Clear()
txtSeptember.Clear()
txtOctober.Clear()
txtNovember.Clear()
txtDecember.Clear()
lblAverage.Clear()
lblTotal.Clear()
'Set focus
txtJanuary.Focus()
End Sub
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub txtAverage_TextChanged(sender As Object, e As EventArgs) Handles lblAverage.TextChanged
End Sub
End Class

Related

vb.net datagridview calculation based on category

How can I calculate the item price based on the category? Currently, I only can calculate the total value. What is the coding in order to do that?
Update:
Below is the code:
Public Class OrderCart
Private Sub OrderCart_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
Dim Price As Double
Dim index As Integer
'calculate total price
For index = 0 To dgvCart.RowCount - 1
Price += dgvCart.Rows(index).Cells(5).Value
Next
lblTotalPriceOutput.Text = FormatCurrency(Price)
End Sub
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBackToHome.Click
Home.Show()
End Sub
Private Sub btnCheckout_Click(sender As Object, e As EventArgs) Handles btnCheckout.Click
Payment.Show()
End Sub
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
Dim i As Integer
'Dim index As Integer
If (dgvCart.RowCount <= 0) Then
MessageBox.Show("No item is selected!", "Remove Item",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
i = dgvCart.CurrentCell.RowIndex
dgvCart.Rows.RemoveAt(i)
End If
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
dgvCart.Rows.Clear()
End Sub
End Class

Visual Studio. Adding numbers to end result

I am working on a project where there are 3 buttons. Add, subtract and exit. When I click the "Add button it should take whatever is in the current "number ordered" box and add it to the total. If I click subtract it should subract from the current total. The buttons work but I can't get the total to stay. If I put in 5, click add, then the total box states 5 but the minute I erase it, the total box erases too. Here is my code:
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Private Sub btnAdd_Click(Sender As Object, e As EventArgs) Handles btnAdd.Click
Dim intNumOrdered As Integer
Dim dblTotal As Double
Integer.TryParse(txtNumOrdered.Text, intNumOrdered)
dblTotal = intNumOrdered + dblTotal
lblTotal.Text = dblTotal.ToString("C2")
End Sub
Private Sub btnSubtract_Click(Sender As Object, e As EventArgs) Handles btnSubtract.Click
Dim intNumOrdered As Integer
Dim dblTotal As Double
Integer.TryParse(txtNumOrdered.Text, intNumOrdered)
dblTotal = dblTotal - intNumOrdered
lblTotal.Text = dblTotal.ToString("C2")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtNumOrdered_Enter(sender As Object, e As EventArgs) Handles txtNumOrdered.Enter
txtNumOrdered.SelectAll()
End Sub
Private Sub txtNumOrdered_TextChanged(sender As Object, e As EventArgs) Handles txtNumOrdered.TextChanged
lblTotal.Text = String.Empty
End Sub
End Class
What do I need to do?
You said:
but the minute I erase [the contents of the numordered box], the total box erases too
And your code says:
Private Sub txtNumOrdered_TextChanged(sender As Object, e As EventArgs) Handles txtNumOrdered.TextChanged
lblTotal.Text = String.Empty
End Sub
To be fair, VB's only doing exactly what you've told it to do! :)
There is a few things wrong here. As pointed out already you are clearing the total box. Also you are not storing the total anywhere and instead using local variables which will initialize to 0. Another thing you should check your TryParse actually succeeds before using the variable.
Public Class frmMain
Private dblTotal As Integer = 0
Private Sub btnAdd_Click(Sender As Object, e As EventArgs) Handles btnAdd.Click
Dim intNumOrdered As Integer
If Integer.TryParse(txtNumOrdered.Text, intNumOrdered) Then
dblTotal = dblTotal + intNumOrdered
lblTotal.Text = dblTotal.ToString("C2")
End If
End Sub
Private Sub btnSubtract_Click(Sender As Object, e As EventArgs) Handles btnSubtract.Click
Dim intNumOrdered As Integer
If Integer.TryParse(txtNumOrdered.Text, intNumOrdered) Then
dblTotal = dblTotal - intNumOrdered
lblTotal.Text = dblTotal.ToString("C2")
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtNumOrdered_Enter(sender As Object, e As EventArgs) Handles txtNumOrdered.Enter
txtNumOrdered.SelectAll()
End Sub
Private Sub txtNumOrdered_TextChanged(sender As Object, e As EventArgs) Handles txtNumOrdered.TextChanged
lblTotal.Text = String.Empty
End Sub
End Class

How to clear Texboxes attached with message boxes in vb.net

For Learning the coding I built this application. It has Three text boxes.1 and 2 to enter numbers and 3 is to sum of 1 and 2. Button 2 is to get sum and 1 to clear textboxes.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text >= 101 Then
MsgBox("Enter numbers between 0 and 100")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox3.Text = Int(TextBox1.Text) + Int(TextBox2.Text)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
End Sub
End Class
Here I have limited the TextBox1 to get numbers between 0 and 100. if it is over 100, displays a warning massage box.
When I click the clear button it gives an error. It doesn't clear. after deleting textbox 1 clearing code it works fine. I mean textbox 2 & 3 clear fine. There is a problem with Textbox 1. The reason i believe is the msgbox attached it. I need to keep Msgbox.
How do I clear them?
You need to suppress text changed event. See TextBoxBase.Clear Method
Public Class Form1
Private flag As Boolean
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
' Check the flag to prevent code re-entry.
If flag = False Then
' Set the flag to True to prevent re-entry of the code below.
If TextBox1.Text >= 101 Then
MsgBox("Enter numbers between 0 and 100")
End If
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox3.Text = Int(TextBox1.Text) + Int(TextBox2.Text)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
flag = True
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
flag = False
End Sub
End Class
TextBox1.Clear() will fire TextBox1_TextChanged().
This line will do an implicit conversion from text to integer - it will fail with an error on blank or text entries:
If TextBox1.Text >= 101 Then
Instead, try this:
Dim number As Integer
If Int32.TryParse(TextBox1.Text, number) Then
If number >= 101 Then
MsgBox("Enter numbers between 0 and 100")
End If
End If

Input Strings into String Arrays(Dynamic)

So I have two string arrays, one for my friends, the other for their numbers. I go to my combo box (displays list of names) I click on it and it displays their number on a label. My problem is I want the user to a enter a new name and number in 2 textboxes, then transfer the name on the combo box. Once their name is in the box, I click on it and it display's their number on label. Is there a way to transfer the new items onto my arrays?
Option Explicit On
Module MainModule
Public strPeople() As String = {"Kyle", "John", "Jake", "Donna", "Carly", "Ty", "Mavis"}
Public strPhoneNumbers() As String = {"945-1232", "804-2329", "290-7321", "928-4569", "205-9893", "320-0195", "305-4520"}
Public tempList As New List(Of String)
End Module
Here Is My Main Form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange(strPeople)
End Sub
Private Sub AboutToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem1.Click
AboutBox1.ShowDialog()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim strPhoneNums As String = strPhoneNumbers(ComboBox1.SelectedIndex)
Label3.Text = "Phone Number: " & strPhoneNums
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
//Add Contact Button
If TextBox1.Text <> "" Then
ReDim Preserve strPeople(7)
strPeople(7) = TextBox1.Text
ComboBox1.Items.Add(strPeople(7))
End If
If TextBox2.Text <> "" Then
ReDim Preserve strPhoneNumbers(7)
strPhoneNumbers(7) = TextBox2.Text
End If
TextBox1.Clear()
TextBox2.Clear()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub

How to store last button clicked and add to listbox

So I'm doing this calculator program and need the numbers, the operator used and the "=" sign to show up in the listbox so "1 + 1 = 2" should show up. I have the calculator working and can move items to the listbox, but can't figure out how to remember the button that was clicked to get the outcome and move it over also.
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & NumTwo.Text & Result.Text)
End Sub
End Class
As far as you just need to store one character, you can rely on the Tag property of ListBox1 (basically, a black box where you can store anything you want). Sample code:
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
ListBox1.Tag = "+"
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
ListBox1.Tag = "-"
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
ListBox1.Tag = "x"
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
ListBox1.Tag = "/"
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & ListBox1.Tag.ToString() & NumTwo.Text & "=" & Result.Text)
End Sub