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
Related
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
I am trying to get a high scores including player name to display on a separate form after all lives are lost in a game. I currently have this code for the high scores form - however it is only displaying the one score in the array and I am wanting to store 10 scores and also player's names.
Imports System.IO
'Code allows the computer to read from the text file containing player scores.'
Public Class Form3
Dim highscore(9) As Integer ' array for highscores
Dim playername(9) As String
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i, value1, value2 As Integer 'player score variables
Dim DataFile As StreamWriter
' Static save As String
value1 = lblScore.Text ' assigns the players score to value1
value2 = lblScore.Text ' assigns the players score to value2
i = 0
DataFile = New StreamWriter("D:\mmend12\My Documents\IPT\Projects\Brandon Moss - Major Project Assignment\Cosmic Crusade\Cosmic Crusade\bin\Debug\HighScore.txt")
i = 0
For i = 0 To 9
If highscore(i) < value1 Then 'checks to see if the players score is bigger than the score
value1 = highscore(i) ' assigns the value of highscore to value 1
highscore(i) = value2 'assigns the players score to the highscore
value2 = value1 ' assigns the previous highscore to value1
End If
lbxHighScores.Items.Add(highscore(i)) ' displays the scores
'lbxNames.Items.Add(playername(i))
DataFile.WriteLine(highscore(i)) ' saves the highscores to disk
' DataFile.WriteLine(playername(i))
Next
DataFile.Close()
End Sub
Private Sub btnShowScores_Click(sender As Object, e As EventArgs) Handles btnShowScores.Click
Dim DataFile As StreamReader
'Declares the variable name "DataFile" as an instance of StreamWriter, meaning that DataFile will behave like a StreamWriter object.'
'Dim J As Integer
DataFile = New StreamReader("D:\mmend12\My Documents\IPT\Projects\Brandon Moss - Major Project Assignment\Cosmic Crusade\Cosmic Crusade\bin\Debug\HighScore.txt")
'Using the file path/address written above, a test.txt file is added in this folder.'
'For J = 1 To 10
'Creates a fixed loop, that loops 10 times
For i = 0 To 9
highscore(i) = DataFile.ReadLine
lbxHighScores.Items.Add(DataFile.ReadLine)
Next
DataFile.Close()
'Closes the data file.
End Sub
I have this code in the game form after all lives are lost:
If PlayersLives = 0 Then
End If
PlayerName = InputBox("Enter your username ")
lblName.Text = PlayerName
Form3.lblScore.Text = lblScoreNumber.Text
Form3.lblPlayer.Text = lblName.Text
Can anyone provide assistance with this.
I would respectfully suggest rewriting the Form3 code as follows ..
I'm assuming that when you click on a name in the ListBox, the associated score is displayed in LblScore
Anyhow back to the code. This way, the code to save and load the scores is separated from your Load event and the code that modifies the list of high scores is also separated out. It will be easier to maintain/modify later. It's also better to manipulate data separately from controls to avoid the risk of inadvertent modification by users.
Each player and their score is now stored in instances of the class Playerand all the players are stored in a List (Of Player)called highScores
There is also now a sub called UpdateListBox which uses the highScores list as a data source. The items to display are the .Name property of each player and the value that is returned when you click on the name of a player is the score which is then assigned to lblScore without any need for looping through arrays or lists.
Public Class Form3
'Code allows the computer to read from the text file containing player scores.'
Friend Class Player
Public Property Score As Integer
Public Property Name As String
End Class
Dim highscores As New List(Of Player) ' array for highscores
Public Sub AddScoreIfHighEnough(player As String, score As Integer)
'make sure that the high score list is sorted first
highscores = highscores.OrderByDescending(Function(x) x.Score).ToList
'work down though the list
For Each plyr As Player In highscores
'if the score is higher than the current item in the list,
'insert the new score there and exit the loop
If score > plyr.Score Then
highscores.Insert(highscores.IndexOf(plyr), New Player With {.Name = player, .Score = score})
Exit For
End If
Next
highscores.Add(New Player With {.Name = player, .Score = score})
'if after the loop has completed, the number of items is
'greater than 10, remove the lowest
If highscores.Count > 10 Then
highscores.Remove(highscores.Last)
End If
UpdateListbox()
SaveScoreData()
End Sub
Private Sub SaveScoreData()
Using DataFile As New StreamWriter("D:\mmend12\My Documents\IPT\Projects\Brandon Moss - Major Project Assignment\Cosmic Crusade\Cosmic Crusade\bin\Debug\HighScore.txt")
For Each plyr As Player In highscores
DataFile.WriteLine(plyr.Name) ' saves the name to disk
DataFile.WriteLine(plyr.Score.ToString) 'saves the score
Next
End Using
End Sub
Private Sub LoadScoreData()
highscores.Clear()
Using DataFile As New StreamReader("D:\mmend12\My Documents\IPT\Projects\Brandon Moss - Major Project Assignment\Cosmic Crusade\Cosmic Crusade\bin\Debug\HighScore.txt")
While Not DataFile.EndOfStream
Dim tempPlayerScore As New Player With {
.Name = DataFile.ReadLine, ' saves the name to disk
.Score = CInt(DataFile.ReadLine) 'saves the score
}
highscores.Add(tempPlayerScore)
End While
End Using
UpdateListbox()
End Sub
Private Sub UpdateListbox()
lbxHighScores.Items.Clear()
For Each plyr As Player In highscores
lbxHighScores.Items.Add(plyr.Name & " " & plyr.Score)
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadScoreData()
End Sub
Private Sub LbxHighScores_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbxHighScores.SelectedIndexChanged
If Not IsNothing(lbxHighScores.SelectedItem) Then
lblScore.Text = CType(lbxHighScores.SelectedItem, Player).Score.ToString
End If
End Sub
End Class
In your code to add a high score, use something like
If PlayersLives = 0 Then
End If
PlayerName = InputBox("Enter your username ")
lblName.Text = PlayerName
form3.AddScoreIfHighEnough(lblName.Text, CInt(lblScoreNumber.Text))
Form3.lblPlayer.Text = lblName.Text
Hello Stackflow community,
I'm currently taking a class in Visual Basic and I'm in need of some assistance. I am creating a program that simulates a Drink Vending Machine using an array. The instructions follow:
Create an application that simulates a soft-drink vending machine. The application should let the user select one of the following soft drinks:
Cola ($1.00 each)
Root Beer ($1.00 each)
Lemon Lime Soda ($1.00 each)
Grape Soda ($1.50 each)
Cream Soda ($1.50 each)
When the application starts, the vending machine will have 20 of each type of soft drink. Each time the user selects a drink, the application should subtract 1 from the quantity of the selected drink. It should also update and display the total amount of sales. If the user selects a drink that is sold out, a message should be displayed indicating so.
In the application’s code, create a structure that has fields for the following data:
Drink name
Drink cost
Number of drinks in machine
The program should create an array of five structure objects. Each element of the array should keep data for a specific type of soft drink.
I can't figure out how to make my buttons work to decrement the quantity labels and then add each click to the label total. Please help.
Public Class Form1
' Class-level declarations
Const intMAX_SUBSCRIPT As Integer = 4 ' Upper subscript
Dim strProdNames(intMAX_SUBSCRIPT) As String ' Product Names
Dim decPrice(intMAX_SUBSCRIPT) As Decimal ' Drink Price
Dim intProdNums(intMAX_SUBSCRIPT) As Integer ' Product Numbers
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Initialize the arrays with the product data.
InitArrays()
End Sub
Private Sub InitArrays()
' Initialize the arrays.
' First Product
strProdNames(0) = "Cola"
decPrice(0) = 1D
intProdNums(0) = 20
' Second Product
strProdNames(1) = "Root Beer"
decPrice(1) = 1D
intProdNums(1) = 20
' Third Product
strProdNames(2) = "Lemon Lime"
decPrice(2) = 1D
intProdNums(2) = 20
' Fourth Product
strProdNames(3) = "Grape Soda"
decPrice(3) = 1.5D
intProdNums(3) = 20
' Fifth Product
strProdNames(4) = "Cream Soda"
decPrice(4) = 1.5D
intProdNums(4) = 20
End Sub
Private Sub btnCola_Click(sender As Object, e As EventArgs) Handles btnCola.Click
Dim intCount As Integer = 20 ' Loop Counter
intCount -= 1 ' Decrement drink count
If intCount > 0 Then
lblTotal.Text = decPrice(0).ToString("c")
End If
End Sub
You need to decrement the cola count, which is in the array.
Private Sub btnCola_Click(sender As Object, e As EventArgs)
If intProdNums(0) > 0 Then
intProdNums(0)-=1 ' decrement cola drink count
End If
' show results here...
End Sub
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
I'm extremely new to vb.net and programming in general so I hope I can get a very simple answer.
I wrote a input box that retrieves user input and stores it in the variable salesQty.
salesQty =
InputBox("Enter the sales quantity for " & itemListBox.Text, "Daily Sales Entry", "0")
Afterwards, I tried to set the text property of a Listbox to equal salesQty (assuming it will change the displayed text for the selected item), but it does not seem to be changing at all.
salesQtyListBox.Text = salesQty
this is the entire code:
Private Sub enterSalesQtyButton_Click(sender As Object, e As EventArgs) Handles enterSalesQtyButton.Click
'Retrieve number of items in itemList Box and sets it as variable numItems
Dim numItems As Integer = itemListBox.Items.Count
'Declare index to use as counter
Dim index As Integer
Do While index < numItems
index = index + 1
Dim salesQty As String
salesQty =
InputBox("Enter the sales quantity for " & itemListBox.Text, "Daily Sales Entry", "0")
'Changes Sales Quantity text to the users input that has been stored in SalesQty
salesQtyListBox.Text = salesQty
'Selects next index until all have been selected
If itemListBox.SelectedIndex < numItems - 1 Then
itemListBox.SelectedIndex = itemListBox.SelectedIndex + 1
End If
Loop
enterSalesQtyButton.Enabled = False
applyDailySalesButton.Enabled = True
End Sub
Private Sub enterSalesQtyButton_Click(sender As Object, e As EventArgs) Handles enterSalesQtyButton.Click
' Declare the varible outside the loop,
' because we dont need a new string every time.
Dim salesQty As String = ""
' Loops through all the items in the list box.
For i As Integer = 0 To itemListBox.Items.Count - 1
' Gets the user quantity for the current item (the "i" item of the list box).
salesQty = InputBox("Enter the sales quantity for " & itemListBox.Items(i), "Daily Sales Entry", "0")
' Changes the "i" item according to the user data.
itemListBox.Items(i) = salesQty
Next
End Sub
To add an item to a listbox you use the following code:
salesQtyListBox.items.add(salesQty)
I'm not sure if that totally answers your question but hopefully that should help.