My code for this program is supposed to add the Variable Entries by +1 when the if function is true however when exporting from the message box it adds 1 and then never changes I tried adding a temp variable and it still didn't work. Problem occurs in the first 3 if functions.
'Application Name: Winter Wonderland Donations
'Developer: Jordan Frahmann
'Date: 1/20/15
'Purpose: This application is used to calculate the total donations
' for ten donations.
Public Class frmWinterWonderlandDonations
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'This button calculates the donations.
Dim decDonation2 As Decimal = 2
Dim decDonation5 As Decimal = 5
Dim decDonation10 As Decimal = 10
Dim Entries As Integer = 0
Dim Donations As Decimal
Dim TotalDonation As Decimal
Dim Temp As Decimal
For Entries = 0 To 10
If rad2.Checked = True Then
Donations = decDonation2
TotalDonation += Donations
Entries += 1
lstNumberOfDonations.Items.Add("Donation " & Entries & Donations & txtName.Text & TotalDonation)
rad2.Checked = False
MsgBox(Entries)
ElseIf rad5.Checked = True Then
Donations = decDonation5
TotalDonation += Donations
Entries = Temp
Entries = Temp + 1
lstNumberOfDonations.Items.Add("Donation " & Entries & Donations & txtName.Text & TotalDonation)
rad5.Checked = False
ElseIf rad10.Checked = True Then
Donations = decDonation10
TotalDonation += Donations
Entries = Entries + 1
lstNumberOfDonations.Items.Add("Donation " & Entries & Donations & txtName.Text & TotalDonation)
rad10.Checked = False
End If
Next
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'This button closes the application and gives a message box.
Close()
MessageBox.Show("Thank you for your donation")
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'This button clears everything in the form.
rad2.Checked = False
rad5.Checked = False
rad10.Checked = False
lstNumberOfDonations.Items.Clear()
txtAddress.Clear()
txtCity.Clear()
txtName.Clear()
txtState.Clear()
txtZipCode.Clear()
End Sub
Private Sub rad5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rad5.CheckedChanged
End Sub
End Class
In your function, given that only rad2 is checked, you are doing
(if rad2.Checked = True),
you are adding values to Entries then you are setting the field to False
rad2.Checked = False
so, in the subsequent loop, the program will just skip the if condition, so the value for Entries is always 1 from the first loop
Do you have any reason for doing
rad2.Checked = false
May be, you can move this outside the loop.
Related
I need help getting the input from the ListBox(labeled lstAge). I need to know so I can convert it to double because I want to make an If statement for the values being used. I will attach what I have in case that helps.
'''
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For count As Double = 16 To 100
count = lstAge.Items.Add(count.ToString())
Next
lstAge.SelectedIndex = 0
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim age As Double
If txtTicket.Text = "N" Or txtTicket.Text = "n" Then
If age < 30 Then
lblResult.Text = txtName.Text & ", your insurance cost will be $45. Keep Up the good driving!"
Else
lblResult.Text = txtName.Text & ", your insurance cost will be $30."
End If
Else
lblResult.Text = txtName.Text & ", your insurance cost will be $85."
End If
End Sub
'''
Try like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For count As Double = 16 To 100
count = lstAge.Items.Add(count.ToString())
Next
lstAge.SelectedIndex = 0
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim age As Double
age = CDbl(lstAge.SelectedItem.ToString())
If txtTicket.Text = "N" Or txtTicket.Text = "n" Then
If age < 30 Then
lblResult.Text = txtName.Text & ", your insurance cost will be $45. Keep Up the good driving!"
Else
lblResult.Text = txtName.Text & ", your insurance cost will be $30."
End If
Else
lblResult.Text = txtName.Text & ", your insurance cost will be $85."
End If
End Sub
In the Button1_Click function, age is set by fetching the value of the selected item in the listbox using ListBox.SelectedItem, converting that to a string, then converting it to a double using CDbl.
This assumes your listbox does not have multiple columns and you are selecting one item at a time.
If i understand you well, you don't know how to get value from ListBox selected item...
To get value from list box, you can use ListBox.SelectedItem property:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim currentage As Integer = DirectCast(lst.SelectedItem, Integer)
Dim message As String = txtName.Text
If txtTicket.Text.ToLower() = "n" Then
Select Case currentage
Case 16 to 30
message &= ", your insurance cost will be $45. Keep Up the good driving!"
Case else
message &= ", your insurance cost will be $30."
End Select
Else
message = &= ", your insurance cost will be $85."
End If
lblResult.Text = message
End Sub
I have been struggling with this code for a while. I'm trying to make it so after the user gets the number right, the program counts how many tries it has taken them to get the number right. How can I do this?
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
'SELECTS THE SECTET NUMBER
Randomize() 'WITHOUT rANDOMIZE, THE SAME SEQUENCE OF NUMBERS APPEARS EACH RUN
secretNumber = Int(Rnd(1) * 1000 + 1) 'picks a random nubmer between 1 and 1000
'sets the initial prompts
Me.lblLow.Text = 1
Me.lblHigh.Text = 1000
Me.txtGuess.Focus()
Dim count As Integer
btnEnterGuess.Enabled = True
btnStart.Enabled = False
Dim i As Integer
For i = 1 To count
count = count + 1
Next i
Do
count = count + 1
Loop Until count = secretNumber
MessageBox.Show("It took you " & i - 1 & " tries to get the number correct.")
End Sub
I threw this piece of code togeather, hopefully it will help.
Something to note when writing this kinda code, is that all this code is running on a single thread (unless you declare it otherwise). This means your program will become unresponsive when you run that for loop.
Public Class Form1
Dim count As Integer = 0
Dim answer As Integer = 0
Dim GameRunning As Boolean = True
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message,
ByVal keyData As System.Windows.Forms.Keys) _
As Boolean
'Gets all keystrokes on the fourm
If GameRunning Then
'converts keystroke to key ID and tests to see if the keystroke is the ENTER key
If msg.WParam.ToInt32() = CInt(Keys.Enter) Then
'try catch to prevent crash if text in entered
Try
If txtAnswer.Text = answer Then
MessageBox.Show("Congratz! It took you " & count & " tries to guess the number!")
Else
txtAnswer.Text = ""
count = (count + 1)
End If
Catch ex As Exception
End Try
End If
End If
'update label to show tries
lblTries.Text = "Tries: " & count
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Private Sub SetupOnLoad() Handles MyBase.Load
Reset()
End Sub
Public Sub Reset()
count = 0
Randomize()
answer = Int(Rnd(1) * 1000 + 1)
txtAnswer.Text = ""
lblTries.Text = "Tries: " & count
GameRunning = True
End Sub
'the Reset() code is ran on the program launch and when you press the button to prepare the game.
Private Sub Reset_Game() Handles BtnReset.Click
Reset()
End Sub
End Class
This code gets the user input in the text box. When the user presses ENTER, the program tests to see if its the correct number.
Hope this helped!
I will try something like this, randomize need to be placed outside of the click
Public Class Form1
Dim count As Integer = 0
Dim answer As Integer = 0
Private Sub btnStart_ClicK(sender As Object, e As EventArgs) Handles btnStart.Click
Randomize()
answer = Int(Rnd(1) * 1000 + 1)
btnStart.Enabled = False
btnGuess.Enabled = True
End Sub
Private Sub btnGuess_Click(sender As Object, e As EventArgs) Handles btnGuess.Click
count += 1
If answer = TextBox1.Text Then
MessageBox.Show("It took you " & count & " tries to get the number correct.")
btnStart.Enabled = True
btnGuess.Enabled = False
count = 0
answer = 0
Else
TextBox1.Text = ""
End If
End Sub
End Class
This program is supposed to accept in valid candidates for voting, add the names typed in a text box to a list box. In the list box the user may double click on the candidate they choose. After the tally button is clicked a list box displaying the candidates' Names and votes will appear along side the other list box.
My problem is that the lstTallies only displays the last voted candidate.
Below is my code
Public Class Form1
Dim maxVotes As Integer
Dim winner As String
Dim votes() As Integer
Dim index As Integer
Dim candidates As String
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
If Not isValidInput(txtNewCandidate.Text) Then
Exit Sub
End If
lstCandidates.Items.Add(txtNewCandidate.Text)
txtNewCandidate.Clear()
txtNewCandidate.Focus()
ReDim Preserve votes(index)
index += 1
End Sub
Private Function isValidInput(ByRef firstName As String) As Boolean
If IsNumeric(txtNewCandidate.Text) Or txtNewCandidate.Text = "" Then
MsgBox("Please input a valid candidate name.")
txtNewCandidate.Focus()
Return False
Else
Return True
End If
End Function
Private Sub btnTally_Click(sender As Object, e As EventArgs) Handles btnTally.Click
lstTallies.Visible = True
lblTally.Visible = True
lstTallies.Items.Add(lstCandidates.Text & " " & votes(lstCandidates.SelectedIndex))
End Sub
Private Sub lstCandidates_DoubleClick(sender As Object, e As EventArgs) Handles lstCandidates.DoubleClick
If lstCandidates.SelectedIndex = -1 Then
MsgBox("Select a candidate by double-clicking")
End If
votes(lstCandidates.SelectedIndex) += 1
MsgBox("Vote Tallied")
End Sub
End Class
Try this:
Assuming the index of the Candidate and his/her Vote are the same:
Private Sub btnTally_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnTally.Click
lstTallies.Visible = True
lblTally.Visible = True
For i = 0 To lstCandidates.Items.Count - 1
lstTallies.Items.Add(lstCandidates.Items(i).ToString & " - " & votes(i))
Next
End Sub
You cannot get the contents of the ListBox unless you iterate it.
The Problem is the coding won't pop up in the Textboxes of the Customer Receipt Form.
Any Solutions?
'This is the coding for the for the SelfCheckOutForm.
'======================================================================================================================================
Option Strict On
'Declare the UPC Structure
Structure UPC
Friend UPCString As String
Friend ProductString As String
Friend DesriptString As String
Friend WeightDecimal As Decimal
Friend PriceDecimal As Decimal
End Structure
Public Class SelfCheckout
'declare structure variables
Dim PriceArray(4) As UPC
'====================================================================================================================================
'declare variables
Friend TotalPriceDecimal As Decimal
Friend TotalWeightDecimal As Decimal
Friend TotalItemsInteger As Integer
Friend MessageString As String
Friend totalString As String
Friend numberOfItemsString As String
Friend weightOfItemsString As String
'=======================================================================================================================================
'ListBox Coding with Arrays
Private Sub SelfCheckout_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Load My List Box with the UPC Codes
UPCListBox.Items.Add("12345")
UPCListBox.Items.Add("23451")
UPCListBox.Items.Add("34512")
UPCListBox.Items.Add("45123")
UPCListBox.Items.Add("51234")
'-------------------------------------------------------------------------------
'Create my UPC Array Values
PriceArray(0).UPCString = "12345"
PriceArray(1).UPCString = "23451"
PriceArray(2).UPCString = "34512"
PriceArray(3).UPCString = "45123"
PriceArray(4).UPCString = "51234"
'-------------------------------------------------------------------------------
'Create my Product Array Values
PriceArray(0).ProductString = "Computer Tower"
PriceArray(1).ProductString = "Laptop"
PriceArray(2).ProductString = "Flatscreen Monitor"
PriceArray(3).ProductString = "Wifi Router"
PriceArray(4).ProductString = "All-In-One Printer"
'------------------------------------------------------------------------------
'Create my Description Array Variables
PriceArray(0).DesriptString = "A computer that can do basic office work."
PriceArray(1).DesriptString = "A computer you can take with you."
PriceArray(2).DesriptString = "A Computer monitor to go with the tower."
PriceArray(3).DesriptString = "You can now go online anywhere in your house!"
PriceArray(4).DesriptString = "It has a Scanner and a fax machine built in!"
'-------------------------------------------------------------------------------
'Create my Price Array Values
PriceArray(0).PriceDecimal = 499.95D
PriceArray(1).PriceDecimal = 550.5D
PriceArray(2).PriceDecimal = 170.95D
PriceArray(3).PriceDecimal = 70.65D
PriceArray(4).PriceDecimal = 199.95D
'------------------------------------------------------------------------------
'Create my Weight Array Values
PriceArray(0).WeightDecimal = 15
PriceArray(1).WeightDecimal = 5
PriceArray(2).WeightDecimal = 8
PriceArray(3).WeightDecimal = 3
PriceArray(4).WeightDecimal = 18
End Sub
'===============================================================================================================================================
'Textbox.text from UPC listbox coding
Private Sub UPCListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UPCListBox.SelectedIndexChanged
'Show UPC in UPC TextBox
UPCTextBox.Text = UPCListBox.Text
'Looking up the Product
'Table Look up Do/Loop
Dim myBoolean As Boolean 'True = Found the Product
Dim indexInteger As Integer = 0
Do Until myBoolean Or indexInteger > 4
With Me
If UPCTextBox.Text = PriceArray(indexInteger).UPCString Then
ProductTextBox.Text = (PriceArray(indexInteger).ProductString) 'Fills Product TextBox
DescriptTextBox.Text = (PriceArray(indexInteger).DesriptString) 'Fill Description TextBox
WeightTextBox.Text = FormatNumber(PriceArray(indexInteger).WeightDecimal) 'Fill Weight Textbox
PriceTextBox.Text = FormatCurrency(PriceArray(indexInteger).PriceDecimal) 'Fill Price Textbox
myBoolean = True
Else
indexInteger += 1
End If
End With
Loop
End Sub
'====================================================================================================================================================
'Message Box Coding
Private Sub PurchaseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseButton.Click
If PurchaseButton.Enabled And PriceTextBox.Text <> "" Then
'Calculate the quantities
TotalItemsInteger += 1 'counts the purchases made
TotalPriceDecimal += CDec(PriceTextBox.Text) 'Adds the purchases together
TotalWeightDecimal += CDec(WeightTextBox.Text) 'Adds the wieghts of purchases together
'----------------------------------------------------------------------------------------------------------------
'Building Meassage Box for purchase button
numberOfItemsString = TotalItemsInteger.ToString()
totalString = TotalPriceDecimal.ToString("C")
weightOfItemsString = TotalWeightDecimal.ToString("N")
MessageString = "Total Items: " & numberOfItemsString &
Environment.NewLine & "Total Weight: " & weightOfItemsString &
Environment.NewLine & "Total Price: " & totalString
'------------------------------------------------------------------------------------------------------------------
'Display Message box
MessageBox.Show(MessageString, "Items Purchased:", MessageBoxButtons.OK)
End If
End Sub
Private Sub CloseOrderButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseOrderButton.Click
CustomerReceiptForm.ShowDialog()
End Sub
End Class
'Programmer: A.Rose
'Date: 10/8/13
'Description: The CustomerReceiptForm provides information on how many items, the total weight and price
'of the combined purchase.
'This is The coding for the CustomerReceiptForm.
'===============================================================================================================================================
Option Strict On
Public Class CustomerReceiptForm
'Function of the Enjoy your Purchase Button
Private Sub CloseBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseBtn.Click
Me.Close()
SelfCheckout.Close()
End Sub
'=================================================================================================================================================
'coding for filling in the Textboxes with the appropriate information
'-----------------------------------------------------------------------
'Total Price of Purchases
Private Sub TotalItemsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalItemsTextBox.TextChanged
TotalItemsTextBox.Text = SelfCheckout.TotalItemsInteger.ToString()
End Sub
'-----------------------------------------------------------------------
'Total Weight of Purchases
Private Sub TotalWeightTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalWeightTextBox.TextChanged
TotalWeightTextBox.Text = SelfCheckout.TotalWeightDecimal.ToString("N")
End Sub
'------------------------------------------------------------------------
'Total Price of Purchases
Private Sub TotalCostTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalCostTextBox.TextChanged
TotalCostTextBox.Text += SelfCheckout.TotalPriceDecimal.ToString("C")
End Sub
'-------------------------------------------------------------------------
End Class
Classes are like blueprints - they tell what something will do when (if) it is ever created. Instances of those classes actually do those things and are what is used in code. You have a CustomerReceiptForm but it is not shown anywhere that you ever created an instance if it like this:
Friend frmCustRec as New CustomerReceiptForm
MAYBE that happens elsewhere in code not posted, but the references in the 2 classes here indicate otherwise. To show it as in the CloseOrderButton_Click it would be
frmCustRec.ShowDialog
The same is true the SelfCheckout class - you need an instance.
BTW, your UPS structure would work great as a private class (think of it as a SelfCheckout helper class). As such, PriceArray could become a List (Of UPC) or Dictionary (of itemKey, UPC) which would make it easier to manage and read.
I am working on a program in Visual Basic that incorporates using a sequential access file for a ballot type program. Each type the user clicks the save vote button, the amount of votes gets stored. What I am trying to do is in my access file is to display the number of votes as an actual number. The way my program is written right now, the name of the candidate appears as many times as the vote was saved. for example, if perez was voted for 4 times, in the access file perez is displayed on 4 different lines. How do I display the actual number of how many time they were voted for. I'm thinking using a counter variable, but not really sure how to really implement it in. this is what i have so far.
Public Class Voter
Dim file As IO.File
Dim infile As IO.StreamReader
Dim outfile As IO.StreamWriter
Private Sub Voter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
outfile = IO.File.CreateText("Votes.txt")
End Sub
Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click
lstResult.Items.Clear()
'which buttons is clicked
If radMark.Checked = True Then
outfile.WriteLine(radMark.Text)
radMark.Checked = False
ElseIf radSheima.Checked = True Then
outfile.WriteLine(radSheima.Text)
radSheima.Checked = False
ElseIf radSam.Checked = True Then
outfile.WriteLine(radSam.Text)
radSam.Checked = False
Else
MessageBox.Show("You should select one among them")
End If
End Sub
Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click
'Dim Mark, Sheima, Sam As Integer
Dim Mark As Integer = 0
Dim Sheima As Integer = 0
Dim Sam As Integer = 0
Dim name As String
'Mark = Sheima = Sam = 0
outfile.Close()
infile = IO.File.OpenText("Votes.txt")
'keep track of votes
While Not infile.EndOfStream
name = infile.ReadLine()
If name.Equals("Mark Stone") Then
Mark += 1
ElseIf name.Equals("Sheima Patel") Then
Sheima += 1
Else
Sam += 1
End If
End While
'results
lstResult.Items.Clear()
lstResult.Items.Add("Mark Stone " & CStr(Mark))
lstResult.Items.Add("Shemia Patel " & CStr(Sheima))
lstResult.Items.Add("Sam Perez " & CStr(Sam))
infile.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.Close()
End Sub
End Class
In your btnVote_Click function you are writing the radiobutton text into the file everytime you click on it, that's how the issue to be created.
Also you are counting how many times the name appears in the file as the number of vote but not the number.
You should try putting the name and count in your vote file rather than just the name, e.g.
Mark,1
Sheima,2
Same,5
Then when you click Vote button, you should read the number for Mark, increment by 1 and write it back.
Try this,
Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click
lstResult.Items.Clear()
'which buttons is clicked
If radMark.Checked = True Then
AddCount(radMark.Text)
radMark.Checked = False
ElseIf radSheima.Checked = True Then
AddCount(radSheima.Text)
radSheima.Checked = False
ElseIf radSam.Checked = True Then
AddCount(radSam.Text)
radSam.Checked = False
Else
MessageBox.Show("You should select one among them")
End If
End Sub
Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click
'results
lstResult.Items.Clear()
lstResult.Items.Add("Mark Stone " & GetCount(radMark.Text))
lstResult.Items.Add("Shemia Patel " & CStr(radSheima.Text))
lstResult.Items.Add("Sam Perez " & CStr(radSam.Text))
End Sub
Private Sub AddCount(Byval VoteName As String)
infile = New IO.File.StreamReader("Votes.txt")
Dim whole_line As String
Dim person As String
Dim vote_count As Integer
Dim found as boolean
'read through the whole file until the end or find the name
Do While infile.Peek() >= 0 And person <> VoteName
'read each line
whole_line = infile.ReadLine
person = Split(whole_line, ",")(0)
If person = VoteName Then
vote_count = Split(whole_line, ",")(1)
found = True
End If
Loop
'Close the file after it is used.
infile.Close()
'Reopen the file with the StreamWriter
outfile = IO.File.OpenText("Votes.txt")
If found = True Then
'the line will only be replace if the person name is found in the line
whole_line = Whole_line.replace(VoteName & "," & vote_count, VoteName & "," & vote_count + 1)
'Write back into the file
outfile.WriteLine(whole_line)
Else
'if the vote name is not found in the file
'Then create a new one
outfile.WriteLine(VoteName & ",1" & VbCrLf)
End If
outfile.Close()
End Sub
Private Function GetCount(Byval VoteName As String)
infile = New IO.File.StreamReader("Votes.txt")
Dim whole_line As String
Dim person As String
Dim vote_count As Integer
'read through the whole file
Do While infile.Peek() >= 0 And person <> VoteName
'read each line
whole_line = infile.ReadLine
person = Split(Whole_line, ",")(0)
If person = VoteName Then
vote_count = Split(whole_line, ",")(1)
End If
Loop
infile.Close()
Return vote_count
End Sub