I am working on a project where there are 3 buttons. Add, subtract and exit. When I click the "Add button it should take whatever is in the current "number ordered" box and add it to the total. If I click subtract it should subract from the current total. The buttons work but I can't get the total to stay. If I put in 5, click add, then the total box states 5 but the minute I erase it, the total box erases too. Here is my code:
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Private Sub btnAdd_Click(Sender As Object, e As EventArgs) Handles btnAdd.Click
Dim intNumOrdered As Integer
Dim dblTotal As Double
Integer.TryParse(txtNumOrdered.Text, intNumOrdered)
dblTotal = intNumOrdered + dblTotal
lblTotal.Text = dblTotal.ToString("C2")
End Sub
Private Sub btnSubtract_Click(Sender As Object, e As EventArgs) Handles btnSubtract.Click
Dim intNumOrdered As Integer
Dim dblTotal As Double
Integer.TryParse(txtNumOrdered.Text, intNumOrdered)
dblTotal = dblTotal - intNumOrdered
lblTotal.Text = dblTotal.ToString("C2")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtNumOrdered_Enter(sender As Object, e As EventArgs) Handles txtNumOrdered.Enter
txtNumOrdered.SelectAll()
End Sub
Private Sub txtNumOrdered_TextChanged(sender As Object, e As EventArgs) Handles txtNumOrdered.TextChanged
lblTotal.Text = String.Empty
End Sub
End Class
What do I need to do?
You said:
but the minute I erase [the contents of the numordered box], the total box erases too
And your code says:
Private Sub txtNumOrdered_TextChanged(sender As Object, e As EventArgs) Handles txtNumOrdered.TextChanged
lblTotal.Text = String.Empty
End Sub
To be fair, VB's only doing exactly what you've told it to do! :)
There is a few things wrong here. As pointed out already you are clearing the total box. Also you are not storing the total anywhere and instead using local variables which will initialize to 0. Another thing you should check your TryParse actually succeeds before using the variable.
Public Class frmMain
Private dblTotal As Integer = 0
Private Sub btnAdd_Click(Sender As Object, e As EventArgs) Handles btnAdd.Click
Dim intNumOrdered As Integer
If Integer.TryParse(txtNumOrdered.Text, intNumOrdered) Then
dblTotal = dblTotal + intNumOrdered
lblTotal.Text = dblTotal.ToString("C2")
End If
End Sub
Private Sub btnSubtract_Click(Sender As Object, e As EventArgs) Handles btnSubtract.Click
Dim intNumOrdered As Integer
If Integer.TryParse(txtNumOrdered.Text, intNumOrdered) Then
dblTotal = dblTotal - intNumOrdered
lblTotal.Text = dblTotal.ToString("C2")
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtNumOrdered_Enter(sender As Object, e As EventArgs) Handles txtNumOrdered.Enter
txtNumOrdered.SelectAll()
End Sub
Private Sub txtNumOrdered_TextChanged(sender As Object, e As EventArgs) Handles txtNumOrdered.TextChanged
lblTotal.Text = String.Empty
End Sub
End Class
Related
How can I calculate the item price based on the category? Currently, I only can calculate the total value. What is the coding in order to do that?
Update:
Below is the code:
Public Class OrderCart
Private Sub OrderCart_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
Dim Price As Double
Dim index As Integer
'calculate total price
For index = 0 To dgvCart.RowCount - 1
Price += dgvCart.Rows(index).Cells(5).Value
Next
lblTotalPriceOutput.Text = FormatCurrency(Price)
End Sub
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBackToHome.Click
Home.Show()
End Sub
Private Sub btnCheckout_Click(sender As Object, e As EventArgs) Handles btnCheckout.Click
Payment.Show()
End Sub
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
Dim i As Integer
'Dim index As Integer
If (dgvCart.RowCount <= 0) Then
MessageBox.Show("No item is selected!", "Remove Item",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
i = dgvCart.CurrentCell.RowIndex
dgvCart.Rows.RemoveAt(i)
End If
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
dgvCart.Rows.Clear()
End Sub
End Class
So I have two string arrays, one for my friends, the other for their numbers. I go to my combo box (displays list of names) I click on it and it displays their number on a label. My problem is I want the user to a enter a new name and number in 2 textboxes, then transfer the name on the combo box. Once their name is in the box, I click on it and it display's their number on label. Is there a way to transfer the new items onto my arrays?
Option Explicit On
Module MainModule
Public strPeople() As String = {"Kyle", "John", "Jake", "Donna", "Carly", "Ty", "Mavis"}
Public strPhoneNumbers() As String = {"945-1232", "804-2329", "290-7321", "928-4569", "205-9893", "320-0195", "305-4520"}
Public tempList As New List(Of String)
End Module
Here Is My Main Form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange(strPeople)
End Sub
Private Sub AboutToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem1.Click
AboutBox1.ShowDialog()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim strPhoneNums As String = strPhoneNumbers(ComboBox1.SelectedIndex)
Label3.Text = "Phone Number: " & strPhoneNums
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
//Add Contact Button
If TextBox1.Text <> "" Then
ReDim Preserve strPeople(7)
strPeople(7) = TextBox1.Text
ComboBox1.Items.Add(strPeople(7))
End If
If TextBox2.Text <> "" Then
ReDim Preserve strPhoneNumbers(7)
strPhoneNumbers(7) = TextBox2.Text
End If
TextBox1.Clear()
TextBox2.Clear()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub
I am struggling with my TryParse method. The program is to calculate the total rainfall and average rainfall. User enters each month in inches. The totals are accurate, but the error message "enter numeric value" pops up when you click calculate. The program also calculates with a symbol or letter, how would i go about stopping it at that point.
Public Class Korszun
Private Sub Korszun_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Please enter each month's rainfall average (ex: 1.11 inches)")
End Sub
Private Sub txtJuly_TextChanged(sender As Object, e As EventArgs) Handles txtJuly.TextChanged
End Sub
Private Sub txtOctober_TextChanged(sender As Object, e As EventArgs) Handles txtOctober.TextChanged
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'declare the variables
Dim intCount As Integer = 0
Dim dblAverageRainfall As Double
Dim dblTotalRainfall As Double
Dim dblInches As Double
'create txtbox variable
'go through each TextBox control in the Groupbox and add them all up
For Each txtBox As TextBox In Me.GroupBox1.Controls.OfType(Of TextBox)()
If Not Double.TryParse(txtBox.Text, dblInches) Then
MessageBox.Show("Please enter numerical values only.")
ElseIf CDbl(txtBox.Text) < 0 Then
MessageBox.Show("Please enter all positive numbers.")
ElseIf TypeOf txtBox Is TextBox Then
'add the total value of rainfall and put it in the decTotalRainfall variable
dblTotalRainfall += CDbl(txtBox.Text)
End If
Next
'get the average
dblAverageRainfall = dblTotalRainfall / 12
'show the results in the labels
lblTotal.Text = dblTotalRainfall.ToString("n2")
lblAverage.Text = dblAverageRainfall.ToString("n5")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'Clear the Textboxes
txtJanuary.Clear()
txtFebruary.Clear()
txtMarch.Clear()
txtApril.Clear()
txtMay.Clear()
txtJune.Clear()
txtJuly.Clear()
txtAugust.Clear()
txtSeptember.Clear()
txtOctober.Clear()
txtNovember.Clear()
txtDecember.Clear()
lblAverage.Clear()
lblTotal.Clear()
'Set focus
txtJanuary.Focus()
End Sub
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub txtAverage_TextChanged(sender As Object, e As EventArgs) Handles lblAverage.TextChanged
End Sub
End Class
I am writing a simple program, the textbook want me to use the string.TrimStart() and string.TrimEnd() methods within the program. You input your amount into txtSales, then select the appropriate sales tax within lstRates, press the calculate button and it calculates how much tax you are charged and displays it in lblTax. Now, seems no matter what method i use do do this, lstRates always returns 0.. Any suggestions?
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 txtSales_Enter(sender As Object, e As EventArgs) Handles txtSales.Enter
txtSales.SelectAll()
End Sub
Private Sub lstRates_SelectedValueChanged(sender As Object, e As EventArgs) Handles lstRates.SelectedValueChanged
lblTax.Text = String.Empty
End Sub
Private Sub txtSales_TextChanged(sender As Object, e As EventArgs) Handles txtSales.TextChanged
lblTax.Text = String.Empty
End Sub
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
' fills the list box with tax rates
For dblRate As Double = 0.02 To 0.1 Step 0.01
lstRates.Items.Add(dblRate.ToString("P0"))
Next dblRate
lstRates.SelectedIndex = 0
End Sub
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Dim dblSales As Double
Dim dblRates As Double
Dim strRates As String
Double.TryParse(txtSales.Text, dblSales)
strRates = lstRates.Text
strRates = strRates.TrimEnd("%"c)
Double.TryParse(strRates, dblRates)
Double.TryParse(lstRates.Text, dblRates)
lblTax.Text = (dblSales * dblRates).ToString
End Sub
End Class
Deleted Double.TryParse(lstRates.Text, dblRates) and added dblRates *= 0.01
Works like a charm!
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Dim dblSales As Double
Dim dblRates As Double
Dim strRates As String
Double.TryParse(txtSales.Text, dblSales)
strRates = lstRates.Text
strRates = strRates.TrimEnd("%"c)
Double.TryParse(strRates, dblRates)
dblRates *= 0.01
lblTax.Text = (dblSales * dblRates).ToString("C2")
End Sub
Public Class Form1
Public digits As String = "0123456789"
Public userInput As String
Public digitCount As Integer = 0
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
userInput = Console.ReadLine()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each i As Char In userInput
If digits.Contains(i) Then digitCount += 1
Next
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub MaskedTextBox1_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected
End Sub
End Class
What should my loop be since this one isn't working and what is the syntax for entering in my digitCount in my maskedtextbox1
Replace your loop with the following simple statement:
digitCount = userInput.Count(Function(c) Char.IsDigit(c))
N.B. Requires reference to System.Linq, which is there by default.