Limiting the range of values allowed in multiple textboxes - vb.net

For my current assignment, I've been asked to create a form to allow customers to enter desired quantities of shoes in a textbox and then total the cost in a second textbox. However, the customer cannot purchase more than 3 of any one particular shoe. I attempted to do the first two shoes in the code below but am running into issues once I added the code for the second model.
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
Dim qntyClassy, qntyHigh As Double
txtQntyClassy.Text = qntyClassy
txtQntyHigh.Text = qntyHigh
If 0 < Int(txtQntyClassy.Text) And Int(txtQntyClassy.Text) < 4 Then
txtTotalClassy.Text = FormatCurrency(txtQntyClassy.Text * 43)
txtQntyClassy.Text = Int(txtQntyClassy.Text)
Else
MsgBox("Enter a quantity no greater than 3.")
End If
If 0 < Int(txtQntyHigh.Text) And Int(txtQntyHigh.Text) < 4 Then
txtTotalHigh.Text = FormatCurrency(txtQntyHigh.Text * 48.95)
txtQntyHigh.Text = Int(txtQntyHigh.Text)
Else
MsgBox("Enter a quantity no greater than 3.")
End If
End Sub
End Class

Related

How to add the sum in the listbox(visual basic) mix with other value, the value should get it price number

from combobox to the dish i used if statement to display my dish in my dish area,
from dish to the list i used:
Dim quantity As Integer
quantity = txtQuantity.Text
If lstDish.SelectedIndex > -1 Then
Dim index1 As Integer
index1 = lstDish.SelectedIndex
Dim item1 As Object
item1 = lstDish.SelectedItem
lstList.Items.Add(item1 & " --> " & quantity)
End If
txtQuantity.Text = 1
The value display at list are will be in form Noodle -> 2
What should i do to get the noodle value as 3.50 * my quantity 2 and add all my sum for the list
Dim Total As Integer
Dim priceOfDish As String = 0
Dim quantity As Integer
Dim friesPrice, garlicBreadPrice, friedChickenWingPrice, springRollPrice As Double
Dim chickenRicePrice, friedRicePrice, curryRicePrice, thaiChickenRice As Double
Dim friedSeafoodNoodlePrice, chickenNoodlePrice, beefNoodlePrice, prawnNoodlePrice As Double
Select Case priceOfDish
Case Is = "Fries"
friesPrice += 2.5
Case Is = "Garlic Bread"
garlicBreadPrice += 2.5
Case Is = "Fried Chicken Wing"
friedChickenWingPrice += 2.5
Case Is = "Spring Roll"
springRollPrice += 2.5
Case Is = "Chicken Rice"
chickenRicePrice += 4
Case Is = "Fried Rice"
friedRicePrice += 4
Case Is = "Curry Rice"
curryRicePrice += 4
Case Is = "Thai Chicken Rice"
thaiChickenRice += 4
Case Is = "Fried Seafood Noodle"
friedSeafoodNoodlePrice += 3.5
Case Is = "Chicken Noodle"
chickenNoodlePrice += 3.5
Case Is = "Beef Noodle"
beefNoodlePrice += 3.5
Case Is = "Prawn Noodle"
prawnNoodlePrice += 3.5
End Select
Total = priceOfDish * quantity
txtTotal.Text = Total
End Sub
code for the total button
I have only included the code to demonstrate one method of getting the total. As several others have mentioned, a Class is probably the best method but I don't think you have reached the use of classes in your course yet.
I have declared 2 form level variables to hold values that can be accessed from more than one method. They do not lose their values at the end of a Sub but maintain them as long as the Form is open. You will need to reset the RunningTotal button to 0 in your Clear button.
When the user selects a category the code resets the value of CategoryCost is set so it is ready if the user chooses to add an item form this category. It is reset every time a new category is chosen.
It is then a simple matter to calculate the cost and add it to the RunningTotal in the AddButton. Then it is displayed in the txtTotal by calling .ToString with the "C2" parameter which stands for Currency with 2 numbers after the decimal point.
Private RunningTotal As Decimal
Private CategoryCost As Decimal
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim Category = ComboBox1.Text
Select Case Category
'Add your code to display the products in each category in the left hand list box
Case "Snack"
CategoryCost = 2.5D
Case "Noodle"
CategoryCost = 3.5D
Case "Rice"
CategoryCost = 4D
Case Else
CategoryCost = 0D
End Select
End Sub
Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
'Your code to display the selected item from the left list box in the right list box
'can be entered here.
RunningTotal += CDec(txtQuantity.Text) * CategoryCost
'If you don't want to display a running total, put the following code in a total button
txtTotal.Text = RunningTotal.ToString("C2")
End Sub
You can create a database for store products and a table for products
By this query
Put like this query on button click event
Declare #sum as integer=0
Select #sum=product_cost * “+val(textboxcount.text)+” from product_table where product_name=“+listbox.selelcttext+” <—-listbox selected
Declare a variable in the form and do your calculation in the Add Button (Not Total Button)
Public Class MyForm
dim MyTotal as Double
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
' Do your stuff to move from dishes to the list
MyTotal = MyTotal + (cdbl(txtQuantity.Text) * Price)
End Sub
Private Sub btnTotal_Click(sender As Object, e As EventArgs) Handles btnTotal.Click
txt_Total.text = MyTotal.ToString("N2")
End Sub
End Class

Lottery Program - Visual Basic

Have to create a lottery program, getting the random numbers and such easily enough. However I'm stumped. Essentially I have 2 buttons. One to display a checked list box with numbers 1-100, along with the 5 lotto numbers. I have a 2nd button that checks 2 things, to make sure that more than 5 numbers are not checked matching numbers. I'm lost on how to check for a match between the selected numbers between the RNG numbers.
Public Class Form1
Private Sub displayBtn_Click(sender As Object, e As EventArgs) Handles displayBtn.Click
Dim lottoNumbers(5) As Integer
Dim counter As Integer = 0
Dim number As Integer
Dim randomGenerator As New Random(Now.Millisecond)
'This will randomly select 5 unique numbers'
Do While counter < 5
number = randomGenerator.Next(0, 98)
If Array.IndexOf(lottoNumbers, number) = -1 Then
lottoNumbers(counter) = number
counter += 1
End If
Loop
'Display the lottery numbers in the label.'
Label1.Text = String.Empty
Array.Sort(lottoNumbers)
For Each num As Integer In lottoNumbers
Label2.Text = "Lottery Numbers"
Label1.Text &= CStr(num) & " "
Next num
For x = 0 To 98
CheckedListBox1.Items.Add(x + 1)
Next
End Sub
Private Sub checkBtn_Click(sender As Object, e As EventArgs) Handles checkBtn.Click
Dim count As Integer = 0
Dim x As Integer
'Checks to see if user checked more than 5'
For x = 0 To CheckedListBox1.Items.Count - 1
If (CheckedListBox1.CheckedItems.Count > 5) Then
MessageBox.Show("You cannot select more than 5 numbers.")
Return
Else
If (CheckedListBox1.GetItemChecked(x) = True) Then
count = count + 1
ListBox1.Items.Add(CheckedListBox1.Items(x))
End If
End If
Next
End Sub

Keeping Track of Totals in Visual Basic

I am trying to code an application that will keep track of the total amount of cars sold through the person's ID number. I have coded it so that the car totals show in their respective fields but how do I get it so that the totals are saved after I input more ID numbers. The below is the ID requirements.
The first number can be either 1 or 2. 1 is new cars sold while 2 is used cars sold.
The last letter of the ID is P (Part time) or F (Full Time).
The setup is like this:
ID: Number of cars sold:
(Textbox to input ID#) (Textbox to input number of cars sold)
Calculate Button Exit Button
Total of cars sold by full-time employees: (label to display total)
Total of cars sold by part-time employees: (label to display total)
Total of new cars sold: (label to display total)
Total of used cars sold: (label to display total)
Here's my code. I want to keep a running total for each depending on the input ID number. So if I put it 1MBP sold 7 cars and 1DKP sold 7 cars, the totals for new cars and part-time employee should show 14
' Name: Huntington Motors Sales Application
' Purpose: The application will calculate the number and type of cars sold as per
' input from the manager. The ID number specifies the type of sales person
' sold the car which is how the application will calculate the total.
' Programmer: Marlinda Davis on October 15, 2015
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Dim strId As String
Dim intNumSold As Integer
Dim intNew As Integer
Dim intUsed As Integer
Dim intPartTime As Integer
Dim intFullTime As Integer
' identify characters in ID '
strId = txtIDNum.Text.Trim.ToUpper
Integer.TryParse(txtNumSold.Text, intNumSold)
' verify that the ID contains a number followed by 3 numbers
' The first number 1 = new car sales person or 2 = used car sales person
' The next two letters are the person's initials
' The last letter indicates if the person is full time(F) or part time(P)
If strId Like "[12][A-Z][A-Z][FP]" Then
If strId.Substring(0) = "1" Then
intNew = intNew + intNumSold
lblNewNum.Text = intNew.ToString
Else
intUsed = intUsed + intNumSold
lblUsedNum.Text = intUsed.ToString
End If
If strId.Substring(3) = "F" Then
intFullTime = intFullTime + intNumSold
lblFTNum.Text = intFullTime.ToString
Else
intPartTime = intPartTime + intNumSold
lblPTNum.Text = intPartTime.ToString
End If
End If
' refocus on ID input box after each entry '
txtIDNum.Focus()
txtIDNum.SelectAll()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Const strEXIT As String = "Are you sure you want to close this application?"
Dim dlgExit As DialogResult
' verify the user wants to close the application '
dlgExit = MessageBox.Show(strEXIT, "Huntington Motors", MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation)
' if the user answers No then the application will not close '
If dlgExit = System.Windows.Forms.DialogResult.No Then
e.Cancel = True
End If
End Sub
End Class

Calculated row on datagrid returning wrong value

I am trying to calculate a total on a row on a datagrid and came up with the following code.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim dblQty As Double
Dim dblPrice As Double
For index As Integer = 0 To grdNewInvoice.RowCount - 1
dblQty += Convert.ToDouble(grdNewInvoice.Rows(index).Cells(1).Value)
dblPrice += Convert.ToDouble(grdNewInvoice.Rows(index).Cells(2).Value)
grdNewInvoice.Rows(index).Cells(3).Value = dblPrice * dblQty
Next
End Sub
Works well if I only have one row. If I have a second row it adds the values of all the columns like this. Column 1 has a combobox that the user can change the value of the quantity, I want the the total row to recalculate everytime the user changes the value. However, it is not calculating correctly, if I change the value on any of the rows, it adds all of the rows together, like this:
I change the value on the wine to 4 and it produced the wrong total
Remove the += otherwise you aggregate the qty and the per price.
9 * $17 = $153
9 = 5 + 4, $17 = $7.00 + $10.00
Code should be:
For index As Integer = 0 To grdNewInvoice.RowCount - 1
dblQty = Convert.ToDouble(grdNewInvoice.Rows(index).Cells(1).Value)
dblPrice = Convert.ToDouble(grdNewInvoice.Rows(index).Cells(2).Value)
grdNewInvoice.Rows(index).Cells(3).Value = dblPrice * dblQty
Next

VB.Net: Grade Calculator Program - Getting Ridiculous Values

I am attempting to create a grade calculator program and I am having some two problems:
Getting the right outputs (because I am getting ridiculous numbers) and
Getting my counters to work.
The basic form is basically one enters 7 input grades:
3 exams (weighed 15%, 20%, and 20% respectively)
A project (weighed 10%),
Assignments (weighed 20%),
Peer reviews (weighed 5%) ,
A programming language presentation (weighed 10%)
and the individual is supposed to get an output of their numeric grade, their letter grade, and two counters that count how many people got A's and F's.
For an example when I enter 3 exam grades: 82,87,91; Assignments: 94; Peer reviews: 100; programming language presentation: 90; and final project: 92,
I get a final numeric grade of 253.90 and a letter grade of F when it clearly should be a letter grade of A and numeric 89.90.
My counters also are not working properly because they aren't displaying the count, but I feel like I put it in the right place (displaying outputs). What exactly am I doing wrong here? Here's my code
Option Strict On
Public Class frmGradeCalculator
'declare Constants
Const EXAM1GRADE_WEIGHT As Decimal = 0.15D
Const EXAM2GRADE_WEIGHT As Decimal = 0.2D
Const EXAM3GRADE_WEIGHT As Decimal = 0.2D
Const HOMEWORKGRADE_WEIGHT As Decimal = 0.2D
Const HOMEWORKPEERREVIEW_WEIGHT As Decimal = 0.05D
Const LANGUAGEQUICKREFERENCE_WEIGHT As Decimal = 0.1D
Const FINALPROJECT_WEIGHT As Decimal = 0.1D
'Declare module variables
Dim mdecFinalNumericGrade As Decimal
Dim mstrFinalLetterGrade As String
Dim mintStudentsWithAs As Integer
Dim mintStudentsWithFs As Integer
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'declare variables
Dim decExam1Grade As Decimal
Dim decExam2Grade As Decimal
Dim decExam3Grade As Decimal
Dim decHomeworkGrade As Decimal
Dim decPeerReviewGrade As Decimal
Dim decLanguageReferenceGrade As Decimal
Dim decFinalProjectGrade As Decimal
Dim decPercent As Decimal
'check for blanks
If (txtExam1Grade.Text) = "" Then
MessageBox.Show("You Can't Leave Exam 1 Blank")
Exit Sub
End If
'check for numeric
If IsNumeric(txtExam1Grade.Text) = False Then 'value is not numeric
MessageBox.Show("Please enter a numeric value for Exam 1!")
Exit Sub
End If
'check for blanks
If (txtExam2Grade.Text) = "" Then
MessageBox.Show("Please enter Exam 2!")
Exit Sub
End If
'check for everything else
If IsNumeric(txtExam2Grade.Text) = False Then 'Value is not numeric
MessageBox.Show("Please enter a numeric value for Exam 2!")
Exit Sub
End If
'check for blanks
If (txtExam3Grade.Text) = "" Then
MessageBox.Show("Please enter Exam 3!")
Exit Sub
End If
'check for numerics
If IsNumeric(txtExam3Grade.Text) = False Then
MessageBox.Show("Please enter a positive numeric value for Exam3!")
Exit Sub
End If
'check for blanks
If (txtHomeworkGrade.Text) = "" Then
MessageBox.Show("Please enter Homework Grade!")
Exit Sub
End If
'check for numerics
If IsNumeric(txtHomeworkGrade.Text) = False Then
MessageBox.Show("Please enter a numeric positive value for Homework Grade!")
Exit Sub
End If
'check for blanks
If (txtPeerReviewGrade.Text) = "" Then
MessageBox.Show("Please enter a Peer Review Grade!")
Exit Sub
End If
'check for numerics
If IsNumeric(txtPeerReviewGrade.Text) = False Then
MessageBox.Show("Please enter a numeric positive value for Peer Review Grade!")
Exit Sub
End If
'check for blanks
If (txtLanguageReferenceGrade.Text) = "" Then
MessageBox.Show("Please enter a Language Reference Grade!")
Exit Sub
End If
'check for numerics
If IsNumeric(txtLanguageReferenceGrade.Text) = False Then
MessageBox.Show("Please enter a numeric positive value for Language Reference Grade!")
Exit Sub
End If
'check for blanks
If (txtFinalProjectGrade.Text) = "" Then
MessageBox.Show("Please enter a Final Project Grade!")
Exit Sub
End If
'check for numerics
If IsNumeric(txtFinalProjectGrade.Text) = False Then
MessageBox.Show("Please enter a numeric positive value for Final Project Grade!")
Exit Sub
End If
'convert data types
decExam1Grade = Convert.ToDecimal(txtExam1Grade.Text)
decExam2Grade = Convert.ToDecimal(txtExam2Grade.Text)
decExam3Grade = Convert.ToDecimal(txtExam3Grade.Text)
decHomeworkGrade = Convert.ToDecimal(txtHomeworkGrade.Text)
decPeerReviewGrade = Convert.ToDecimal(txtPeerReviewGrade.Text)
decLanguageReferenceGrade = Convert.ToDecimal(txtLanguageReferenceGrade.Text)
decFinalProjectGrade = Convert.ToDecimal(txtFinalProjectGrade.Text)
mdecFinalNumericGrade = (decExam1Grade * EXAM1GRADE_WEIGHT) + _
(decExam2Grade * EXAM2GRADE_WEIGHT) + _
(decExam3Grade * EXAM3GRADE_WEIGHT) + _
(decHomeworkGrade * HOMEWORKGRADE_WEIGHT) + _
(decPeerReviewGrade * HOMEWORKPEERREVIEW_WEIGHT) + _
(decLanguageReferenceGrade + LANGUAGEQUICKREFERENCE_WEIGHT) + _
(decFinalProjectGrade + FINALPROJECT_WEIGHT)
'check for 0 or positive
If decExam1Grade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Exam 1!")
Exit Sub
End If
'check for 0 or positive
If decExam2Grade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Exam 2!")
Exit Sub
End If
'check for 0 or positive
If decExam3Grade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Exam 3!")
Exit Sub
End If
'check for 0 or positive
If decHomeworkGrade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Homework Grade!")
Exit Sub
End If
'check for 0 or positive
If decPeerReviewGrade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Peer Review Grade!")
Exit Sub
End If
'check for 0 or positive
If decLanguageReferenceGrade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Language Reference!")
Exit Sub
End If
'check for 0 or positive
If decFinalProjectGrade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Final Project Grade!")
Exit Sub
End If
'make sure values are less than 100
If decExam1Grade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
If decExam2Grade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
If decExam3Grade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
If decHomeworkGrade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
If decPeerReviewGrade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
If decLanguageReferenceGrade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
If decFinalProjectGrade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
'Determine grade per letter
Select Case decpercent
Case Is >= 89.5D
mstrFinalLetterGrade = "A"
mintStudentsWithAs += 1
Case Is >= 79.5D
mstrFinalLetterGrade = "B"
Case Is >= 69.5D
mstrFinalLetterGrade = "C"
Case Is >= 59.5D
mstrFinalLetterGrade = "D"
Case Else
mstrFinalLetterGrade = "F"
mintStudentsWithFs += 1
End Select
lblFinalLetterGrade.Text = mstrFinalLetterGrade
'display outputs
lblFinalNumericGrade.Text = mdecFinalNumericGrade.ToString("f2")
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'clear the texboxes and labels
txtExam1Grade.Clear()
txtExam2Grade.Clear()
txtExam3Grade.Clear()
txtHomeworkGrade.Clear()
txtPeerReviewGrade.Clear()
txtLanguageReferenceGrade.Clear()
txtFinalProjectGrade.Clear()
lblFinalLetterGrade.Text = ""
lblFinalNumericGrade.Text = ""
'setcursor back to top textbox
txtExam1Grade.Focus()
End Sub
Private Sub btnReset_Click(ByVal sende As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
'reset module level variables
mdecFinalNumericGrade = 0
mstrFinalLetterGrade = ""
mintStudentsWithAs = 0
mintStudentsWithFs = 0
End Sub
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
'close the form
End
End Sub
Private Sub frmGradeCalculator_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Your final grade total is wrong because you're not multiplying the language presentation and final project by their weights, but adding the weight to the score:
Exam1 = 82 * .15 = 12.3
Exam2 = 87 * .20 = 17.4
Exam3 = 91 * .20 = 18.2
Assignments = 94 * .20 = 18.8
Peer Review = 100 * .05 = 5
Language Presentation = 90 + .10 = 90.1
Final Project = 92 + .10 = 92.1
Notice the addition (rather than multiplication) on the last two values. The total is 253.9.
Change the last two calculations for medcFinalNumericGrade to:
(decLanguageReferenceGrade * LANGUAGEQUICKREFERENCE_WEIGHT) + _
(decFinalProjectGrade * FINALPROJECT_WEIGHT)
You will always get an "F" because decpercent is never assigned a value, and therefore the Else Case is executed. Either assign decpercent a value, or use mdecFinalNumericGrade.
For example:
Select Case mdecFinalNumericGrade
instead of
Select Case decpercent
Once you fix the Select Case, your counts should work (unless you reset the form).