VB textbox.enter gives null value (accumulator) - vb.net

Have I simple VB program which accumulating via TextBox.Enter fails. Goal is: how fix TextBox.Enter? I do not get MsgBox, indicating that action tree has not been followed.
Option Explicit On
Public Class MainForm
Public decexpenses, decincome As Decimal
Public dectotalexpenses As Decimal = 0
Public dectotalincome As Decimal = 0
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox("Use Enter key to enter values.")
End Sub
Private Sub expensesTextBox_Enter(sender As Object, e As EventArgs) Handles expensesTextBox.Enter
Do Until expensesTextBox.Text = String.Empty
Dim expenses = expensesTextBox.Text
MsgBox(expenses) ' i dont get a msgbox for this indicating this value is null
Do Until IsNumeric(expenses)
MsgBox("Please enter numeric value for expenses.")
Loop
decexpenses = CDec(expenses)
dectotalexpenses = decexpenses + dectotalexpenses
Loop
End Sub
Private Sub incomeTextBox_Enter(sender As Object, e As EventArgs) Handles incomeTextBox.Enter
Do Until incomeTextBox.Text = String.Empty
Dim income = incomeTextBox.Text
Do Until IsNumeric(income)
MsgBox("Please enter numeric value for income.")
Loop
decincome = CDec(income)
dectotalincome = decincome + dectotalexpenses
Loop
End Sub
End Class

Couldn't you just right a method that does the below? It appears that all your looking is to check to make sure it is an integer that was entered into the textbox and if it is calculate it, otherwise display a messagebox to tell the user to enter a number.
Private Sub expensesTextBox_Enter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles expensesTextBox.KeyPress
Dim dectotalexpenses As Decimal = 0
Dim dectotalincome As Decimal = 0
Dim income As String = expensesTextBox.Text
If Keys.E + Keys.Enter Then
If IsNumeric(income) = True Then
dectotalincome = income + dectotalexpenses
Else
MessageBox.Show("Please Enter A Number")
End If
End If
End Sub

Related

The input String is not in correct format even though my variable is an integer

Private Sub shirtnum_KeyPress(sender As Object, e As KeyPressEventArgs) Handles shirtnum.KeyPress ("this is suppose to type in any number using into the textbox using your keyboard")
Dim Ch As Char = e.KeyChar
If Not Char.IsDigit(Ch) AndAlso Asc(Ch) <> 8 Then
e.Handled = True
End If
End Sub
Private Sub addshbtn_Click(sender As Object, e As EventArgs) Handles addshbtn.Click
Dim t As Integer = Integer.Parse(shirtnum.Text) ("this is to add 1 number to the textbox")
t += 1
shirtnum.Text = t
addshbtn.Enabled = True
pricetxt_TextChanged(sender, e)
End Sub
Private Sub minusshbtn_Click(sender As Object, e As EventArgs) Handles minusshbtn.Click ("this is to minus 1 number from the textbox")
Dim t As Integer = Integer.Parse(shirtnum.Text)
t += -1
If t <= 0 Then
t = 0
End If
shirtnum.Text = t
pricetxt_TextChanged(sender, e)
End Sub
Private Sub shirtnum_TextChanged(sender As Object, e As EventArgs) Handles shirtnum.TextChanged
End Sub
Private Sub pricetxt_TextChanged(sender As Object, e As EventArgs) Handles pricetxt.TextChanged
"Dim t As Integer = Integer.Parse(shirtnum.Text) -- this is where the error appears **NOTE** this this code works without this sub"
t = 1.0
pricetxt.Text = ("$" & t * 15 & ".00")
pricetxt.BorderStyle = 0
pricetxt.BackColor = Me.BackColor
End Sub
Got an issue with my assignment is that the code appears to say that the input string is not in the correct format, even though my integer is a number.
If someone can help me out with this, this would be a big help, thanks!

Creating a Grade Calculator in Visual Studio's 2019 (VB) and struggling with the code a bit

I am using VB.Net in Visual Studio 2019 to help accumulate test score data.
I have a Label named Score, and Textboxes for Score Total, Score Count, and Score Average that are all read only. I also button to Add and Clear scores, as well as Exit.
I have the code done for the Clear Scores and Exit buttons, but I am struggling with the Add button and getting all the scores input and summed.
My goal is to display the sum of all the scores in the Score Total box, the number of scores in the Score Count box, and their average in the Score Average box.
This is what I have so far:
Public Class GradeCalculator
Dim score As Integer
Dim ScoreTotal As Decimal
Dim ScoreCount As Decimal
Dim Average As Integer
Private Sub frmClearScores_Click(sender As Object, e As EventArgs) Handles frmClearScores.Click
score = 0
ScoreTotal = 0
ScoreCount = 0
Average = 0
txtscore.Text = ""
txtscoretotal.Text = ""
txtscorecount.Text = ""
txtaverage.Text = ""
txtscore.Select()
End Sub
' This is the "Add" button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
How can I finish this?
Public Class GradeCalculator
Dim ScoreTotal As Integer = 0
Dim ScoreCount As Integer = 0
Private Sub frmClearScores_Click(sender As Object, e As EventArgs) Handles frmClearScores.Click
ScoreTotal = 0
ScoreCount = 0
txtscore.Text = ""
txtscoretotal.Text = ""
txtscorecount.Text = ""
txtaverage.Text = ""
txtscore.Select()
End Sub
' This is the "Add" button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ScoreTotal += CInt(txtscore.Text)
ScoreCount += 1
txtscore.Text = ""
txtscoretotal.Text = ScoreTotal.ToString()
txtscorecount.Text = ScoreCount.ToString()
txtaverage.Text = (CDec(ScoreTotal)/ScoreCount).ToString()
txtscore.Select()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class

Putting value from Combobox Item to Textbox in vb.net "Conversion from string "" to type 'Double' is not valid."

Public Class Main
Dim Adult As Integer = 495 'Adult as Integer
Dim Senior As Integer = 395 'Senior as Integer
Private Sub Combo() 'New created Sub
Dim Aval = Combad.Text 'Aval as Combo box Adult
Dim Sval = Comse.Text 'Sval as Combo box Senior
Textpay.Text = Adult * Aval + Senior * Sval 'Adding up the total of both Combo Box's Value
End Sub
Private Sub Combad_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Combad.SelectedIndexChanged
Combo() 'Private Sub
End Sub
Private Sub Comse_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Comse.SelectedIndexChanged
Combo() 'Private Sub
End Sub
End Class
First check if your combobox is empty or not cause you will get an error if it is (by using if Aval <> string.empty) second of all you need to check if the combobox is a number, you can use If Integer.TryParse(Aval, Nothing). Use AndAlso if you want to check both at the same time if Aval <> string.empty AndAlso Integer.TryParse(Aval, Nothing)
You parse the String values in the .Text property with .TryParse. The method will return True and assign the Integer value to the provided variable if the conversion is possible.
Private Adult As Integer = 495
Private Senior As Integer = 395
Private Sub Combo()
Dim Aval As Integer
Dim Sval As Integer
If Integer.TryParse(Combad.Text, Aval) AndAlso Integer.TryParse(Comse.Text, Sval) Then
Textpay.Text = (Adult * Aval + Senior * Sval).ToString
Else
MessageBox.Show("Both drop down boxes must display a number.")
End If
End Sub
EDIT
Here is the complete code that I just tested.
Private Adult As Integer = 495
Private Senior As Integer = 395
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange({1, 2, 3, 4})
ComboBox2.Items.AddRange({1, 2, 3, 4})
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Combo()
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
Combo()
End Sub
Private Sub Combo()
Dim Aval As Integer
Dim Sval As Integer
If Integer.TryParse(ComboBox1.Text, Aval) AndAlso Integer.TryParse(ComboBox2.Text, Sval) Then
TextBox1.Text = (Adult * Aval + Senior * Sval).ToString
Else
MessageBox.Show("Both drop down boxes must display a number.")
End If
End Sub

Visual Basic listbox with loop to determine a total score and average

I am new to VB and I need to create a program for a skating rink. The number of judges varies from week to week and at the end of each skater's routine each judge assigns a score 0-10 to the skater. The application needs to allow the manger of the rink to enter each judge's score for a specific skater. It should also calculate and display the skater's average, the number of scores entered, and the total score. You select each score from a List box and then click the Record Score button.
I have no idea where to go next. Everything is incorrect when you select the first score from the list box and its also re-adding the score to the bottom of the list box. Below is the code and any help is appreciated.
Thanks!
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
'Fill list box with values
For intScore As Integer = 0 To 10 Step 1
lstScore.Items.Add(intScore.ToString)
Next intScore
lstScore.SelectedItem = "0"
End Sub
Private Sub btnRecordScore_Click(sender As Object, e As EventArgs) Handles btnRecordScore.Click
'accumulates the scores for each skater
Dim intScore As Integer
Dim intNumScores As Integer
Dim intTotalScore As Integer
Dim decAvgScore As Decimal
Integer.TryParse(lstScore.SelectedItem.ToString, intScore)
'add score to score
lstScore.Items.Add(lstScore.SelectedItem)
'update average
intNumScores = 0
intTotalScore = 0
For Each selScore As Object In lstScore.Items
If Integer.TryParse(selScore.ToString, intScore) Then
intNumScores = intNumScores + intScore
intTotalScore = intTotalScore + 1
End If
Next
decAvgScore = intNumScores \ intTotalScore
lblTotalScore.Text = intNumScores.ToString
lblNumScores.Text = intTotalScore.ToString
lblAvgScore.Text = decAvgScore.ToString("N1")
End Sub
Private Sub btnNextSkater_Click(sender As Object, e As EventArgs) Handles btnNextSkater.Click
'clears screen, sets focus
lblTotalScore.Text = String.Empty
lblNumScores.Text = String.Empty
lblAvgScore.Text = String.Empty
lstScore.Focus()
End Sub
End Class
Try this out.
Public Class Form1
'Declare TotalScore and NumScore here so they're values stay constant
Dim intTotalScore As Integer = 0
Dim intNumScores As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Populate list box with scores
For intScore As Integer = 0 To 10 Step 1
lstScore.Items.Add(intScore.ToString)
Next intScore
lstScore.SelectedItem = "0"
End Sub
Private Sub btnRecordScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecordScore.Click
Dim intScore As Integer
Dim AvgScore As Decimal
'add score to score
intScore = lstScore.SelectedIndex
intTotalScore = intTotalScore + lstScore.SelectedIndex
'update average
intNumScores = intNumScores + 1
AvgScore = intTotalScore / intNumScores
'Populate labels
lblTotalScore.Text = intTotalScore.ToString
lblNumScores.Text = intNumScores.ToString
lblAvgScore.Text = AvgScore.ToString
End Sub
Private Sub btnNextSkater_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextSkater.Click
'clears screen, sets focus
intTotalScore = 0
intNumScores = 0
lblTotalScore.Text = String.Empty
lblNumScores.Text = String.Empty
lblAvgScore.Text = String.Empty
lstScore.SelectedItem = "0"
lstScore.Focus()
End Sub
End Class
Remember that lstScore.Items.Add(lstScore.SelectedItem) will only add another item to the listbox.
Hope this helps.

Windows Form won't pass a variable to another?

my problem is that is have 4 forms, 3 of these forms allow me to pass variables between them.. however one form the Booking form won't allow me to pass the txtTotal variable to the Confirmation Form.
All the other forms all me to do this, im not doing anything different i can see... im thinking that perhaps another part of the form is prohibiting me from passing that txtTotal to the Confirmatin form.
The following is for the Confirmation form, it should display the txtTotal in lblprice from the Booking form but shows nothing
Public Class Confirmation
Private Sub btnBack_Click(sender As System.Object, e As System.EventArgs) Handles btnBack.Click
Dim FrmPayment As New Payment
FrmPayment.Show()
Me.Hide()
End Sub
Private Sub btnHome_Click(sender As System.Object, e As System.EventArgs) Handles btnHome.Click
Dim FrmSelection As New Selection
FrmSelection.Show()
Me.Hide()
End Sub
Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles lblshow.Click, lbltime.Click, lbldate.Click, lblcust.Click, lblprice.Click
End Sub
Private Sub Confirmation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'lblprice displays nothing and the rest of the labels display the correct values
lblprice.Text = Booking.txtTotal.Text
lblshow.Text = Selection.cboShowSelect.Text
lbldate.Text = Selection.cboDateSelect.Text
lbltime.Text = Selection.cboTimeSelect.Text
End Sub
End Class
Here is all the code in the Booking form if it helps
Public Class Booking
Private Sub Booking_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'labels 1,4,6 display information from the comboboxes on the Selection Form
Label1.Text = Selection.cboShowSelect.Text
Label4.Text = Selection.cboDateSelect.Text
Label6.Text = Selection.cboTimeSelect.Text
Dim i As Integer
For i = 1 To 4
cboAdult.Items.Add(i)
cboChild.Items.Add(i)
cboSenior.Items.Add(i)
cboStudent.Items.Add(i)
Next i
End Sub
Public Sub ComboBoxes_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboAdult.SelectedIndexChanged, cboChild.SelectedIndexChanged, cboSenior.SelectedIndexChanged, cboStudent.SelectedIndexChanged
'Assigned an evet handler to all of the comboboxes then calculates the price and puts in total box
Dim Totalcombo1, Totalcombo2, Totalcombo3, Totalcombo4, Price As Decimal
Dim valuecombo1 = (cboAdult.SelectedIndex + 1) 'finds position of option selected & adds one to get number of tickets
Dim valuecombo2 = (cboChild.SelectedIndex + 1)
Dim valuecombo3 = (cboSenior.SelectedIndex + 1)
Dim valuecombo4 = (cboStudent.SelectedIndex + 1)
'if the submit button is selected without there being a value selected from any combobox then error should appear, saying at least 1 ticket should be purchased.
If (cboChild.SelectedIndex = -1) Then
Totalcombo2 = 0
Else
Price = 6.5
Totalcombo2 = valuecombo2 * Price
End If
'determines the ticketprice of combobox 1
If (cboAdult.SelectedIndex = -1) Then
Totalcombo1 = 0
Else
Price = 9
Totalcombo1 = valuecombo1 * Price
End If
'determines the ticketprice of combobox 2
If (cboSenior.SelectedIndex = -1) Then
Totalcombo3 = 0
Else
Price = 6.5
Totalcombo3 = valuecombo3 * Price
End If
'determines the ticketprice of combobox 3
If (cboStudent.SelectedIndex = -1) Then
Totalcombo4 = 0
Else
Price = 6.5
Totalcombo4 = valuecombo4 * Price
End If
'determines the ticketprice of combobox 4
If (cboAdult.SelectedIndex = -1 And cboChild.SelectedIndex = -1 And cboSenior.SelectedIndex = -1 And cboStudent.SelectedIndex = -1) Then
MessageBox.Show("Please make at least one ticket selection before continuing. ")
End If
txtTotal.Text = Totalcombo1 + Totalcombo2 + Totalcombo3 + Totalcombo4
'adds the totals of the ticketprices and then inserts into the Total label
End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboChild.SelectedIndexChanged
End Sub
Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
Dim FrmSelection As New Selection
FrmSelection.Show()
Me.Hide()
End Sub
Sub Form_OpenBooking(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If (cboChild.SelectedIndex >= 0 And (cboSenior.SelectedIndex = -1 And cboAdult.SelectedIndex = -1)) Then
MessageBox.Show("A child must be accompanied by at least one adult", "Invalid Selection")
ElseIf (txtTotal.Text > 0) Then 'if the total label is greater than zero then this means at least one ticket selection has been made
Dim Form3 As New Payment ' if above is true open Booking Form
Form3.Show()
Me.Hide()
End If
End Sub
Private Sub Resetbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
cboAdult.SelectedIndex = -1
cboChild.SelectedIndex = -1
cboSenior.SelectedIndex = -1
cboStudent.SelectedIndex = -1
txtTotal.Text = 0
End Sub
End Class
Thanks in advance!
Try this
Public frm as new frmBooking
frm.txtTotal.Text=
It should work
You need to use the actual instance of the Form that you created, not the Class name. You should also make sure that you turn on Option Strict.
Try frmBooking.txtTotal.Text instead of Booking.txtTotal.Text