recall array in Form load event - vb.net

I have a form with a textbox. If I type in "91" it should give me grade A and so forth. If I put my array in my button click it works. However, if I put it in my form load event, it only works once. Do I have to recall it everytime?
Public Class Form1
Structure Grades
Public intMinGrade As Integer
Public strLetterGrade As String
End Structure
Dim graGradingScale(5) As Grades
Dim index As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'For i As Integer = 0 To graGradingScale.Length - 1
' MessageBox.Show(graGradingScale(i).intMinGrade)
'Next
Dim FoundBoolean As Boolean = False
Do Until FoundBoolean Or index > 5
If TextBox1.Text = graGradingScale(index).intMinGrade.ToString Then
Label1.Text = graGradingScale(index).strLetterGrade.ToString
FoundBoolean = True
Else
index += 1
End If
Loop
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
graGradingScale(0).intMinGrade = 91
graGradingScale(0).strLetterGrade = "A"
graGradingScale(1).intMinGrade = 81
graGradingScale(1).strLetterGrade = "B"
graGradingScale(2).intMinGrade = 71
graGradingScale(2).strLetterGrade = "C"
graGradingScale(3).intMinGrade = 61
graGradingScale(3).strLetterGrade = "D"
graGradingScale(4).intMinGrade = 0
graGradingScale(4).strLetterGrade = "F"
End Sub
End Class

Insert the Dim index As Integer inside the Button1_Click
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim index As Integer = 0
'code continues
valter

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

I want to display a list of names(3) from a textbox(user input) into a label

This is the code I have so far but is not working properly. The textbox for the names user should input and the button show to display the names in a label in the order entered.
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim intcount, w1, w2, w3 As Integer
Dim intMax As Integer = 2
For intcount = 0 To intMax
strSurnames(intcount) = TextBox1.Text
Next
TextBox1.Clear()
End Sub
Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
Dim intMax As Integer = 2
For intcount = 0 To intMax
lblShow.Text &= strSurnames(intcount)
Next
End Sub
I am guessing you are going to use only 1 textbox and click the "add button" multiple times to store the name. If this is true, you will first need to create
Dim arrayStr As New List(Of String)
Every single time you click on the add button, it will add into this array.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
arrayStr.Add(TextBox1.Text.Trim())
TextBox1.Clear()
End Sub
And to show the full name in just one label when you click on the "Show button", can do it like this
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = "" //The label text is cleared.
For i As Integer = 0 To arrayStr.Count() - 1
Label1.Text += arrayStr(i) + ", "
Next
End Sub
UPDATE - BASED ON YOUR COMMENT QUESTION
This is the updated solution based on your question. I am only going to show you with the "Sur Names" only. You can implement the "Weight" the same way.
First, create your array and declare a count as integer for the size of your array.
Dim surNameStr(20) As String
Dim count As Integer = 0
In the "Add Button", you increase the count number by 1 every time you add a new sur name. Once it reached your "maximum" number, you disable your button by BtnAdd.Enabled = False.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If count < 20 Then
surNameStr(count) = TextBox1.Text.Trim()
count = count + 1
Else
Button1.Enabled = False
Button2.Enabled = True
End If
End Sub
Then, in "Show Button", this is how you can show all the surnames stored.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = ""
For i As Integer = 0 To 19
Label1.Text += surNameStr(i) + vbNewLine
Next
End Sub

calculation is only performed once on food ordering form (VB)

I am making a food ordering form, and I am having trouble with the calculations. Every time a hotdog is ordered the form is supposed to add the cost to the total amount, so one hotdog is 2 dollars, add another and it says 4 dollars, etc. But it only performs the calculation once, so no matter how many items I add to the order the price stays the same, as if I had only ordered one.
I don't get any errors at runtime so it must be a logical error. Here is the full code. I don't know what other information I should give at this moment. All help is appreciated.
Public Class DroneDogs
Dim DogChoice As String
Dim condiment1 As String
Dim condiment2 As String
Dim condiment3 As String
Const DBL_TAX_RATE As Double = 0.07
Const DBL_DRONE_DOG As Double = 1.99
Dim intNumDog As Integer
Dim dblSubTotal As Double
Dim dblSalesTax As Double
Dim dblTotalCost As Double
Private Sub btnAddCustomer_Click(sender As Object, e As EventArgs) Handles btnAddCustomer.Click
cboCurrentCustomers.Items.Add(txtAddCustomer.Text)
End Sub
Private Sub optBeef_CheckedChanged(sender As Object, e As EventArgs) Handles optBeef.CheckedChanged
If (optBeef.Checked) Then
DogChoice = "Beef Dog"
End If
End Sub
Private Sub optPork_CheckedChanged(sender As Object, e As EventArgs) Handles optPork.CheckedChanged
If (optPork.Checked) Then
DogChoice = "Pork Dog"
End If
End Sub
Private Sub optTurkey_CheckedChanged(sender As Object, e As EventArgs) Handles optTurkey.CheckedChanged
If (optTurkey.Checked) Then
DogChoice = "Turkey Dog"
End If
End Sub
Private Sub chkKetchup_CheckedChanged(sender As Object, e As EventArgs) Handles chkKetchup.CheckedChanged
If (chkKetchup.Checked) Then
condiment1 = " + Ketchup"
Else
condiment1 = Nothing
End If
End Sub
Private Sub chkMustard_CheckedChanged(sender As Object, e As EventArgs) Handles chkMustard.CheckedChanged
If (chkMustard.Checked) Then
condiment2 = " + Mustard"
Else
condiment2 = Nothing
End If
End Sub
Private Sub chkRelish_CheckedChanged(sender As Object, e As EventArgs) Handles chkRelish.CheckedChanged
If (chkRelish.Checked) Then
condiment3 = " + Relish"
Else
condiment3 = Nothing
End If
End Sub
Private Sub btnAddDog_Click(sender As Object, e As EventArgs) Handles btnAddDog.Click
intNumDog = +1
dblSubTotal = intNumDog * DBL_DRONE_DOG
dblSalesTax = dblSubTotal * DBL_TAX_RATE
dblTotalCost = dblSubTotal + dblSalesTax
txtSub.Text = dblSubTotal.ToString("c2")
txtTax.Text = dblSalesTax.ToString("c2")
txtTotal.Text = dblTotalCost.ToString("c2")
Dim addCondiment As String = condiment1 + condiment2 + condiment3
If (DogChoice = "Beef Dog") Or
(DogChoice = "Pork Dog") Or
(DogChoice = "Turkey Dog") Then
lstOrder.Items.Add(DogChoice + addCondiment + ": " + txtTotal.Text)
Else
MsgBox("Please select a Hot Dog type.")
End If
End Sub
Private Sub btnClearOrder_Click(sender As Object, e As EventArgs) Handles btnClearOrder.Click
lstOrder.Items.Clear()
txtSub.Clear()
txtTax.Clear()
txtTotal.Clear()
chkKetchup.Checked = 0
chkMustard.Checked = 0
chkRelish.Checked = 0
optBeef.Checked = 0
optPork.Checked = 0
optTurkey.Checked = 0
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Exits the program
If MessageBox.Show("Do you want to exit the DroneDogs application?", "DroneDogs",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
= DialogResult.Yes Then
Application.Exit()
End If
End Sub
Private Sub btnOrderComplete_Click(sender As Object, e As EventArgs) Handles btnOrderComplete.Click
Receipt.Show()
End Sub
Private Sub btnRemoveDog_Click(sender As Object, e As EventArgs) Handles btnRemoveDog.Click
lstOrder.Items.Remove(lstOrder.SelectedItem)
End Sub
End Class
I think the problem is with the line
intNumDog = +1
In this line, you always set intNumDog to the value of +1. This is the same as:
intNumDog = 1
You want to add one to the value of intNumDog:
intNumDog += 1

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