TextBox TextChanged Error - vb.net

My code needs a little work
Public Class Form1
Dim Bread, TotalPrice As Double
Private Sub txtBread_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBread.TextChanged
If txtBread.Text = "" Then
TotalPrice = TotalPrice - Bread
lblBread.Text = Bread.ToString
lblPrice.Text = TotalPrice.ToString
Else
Bread = Val(txtBread.Text) * 3.25
lblBread.Text = Bread.ToString
TotalPrice = TotalPrice + Bread
lblPrice.Text = TotalPrice.ToString
End If
End Sub
End Class
My textbox is good for a one-digit number only.
So my error here is when i input two-digit numbers on my text box, it actually updates my labels, but when i press backspace it does not update anymore.

The value of the variable TotalPrice grows with each new input (no matter if it is bigger or smaller than the previous one) and thus the value of lblPrice.Text. For example:
txtBread.Text TotalPrice
1 1
15 16
1 17
If you explain what you want to accomplish exactly, I can update your code.
Dim Bread As Double
Dim TotalPrice as Double = 5 'Any constant value you want
Private Sub txtBread_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBread.TextChanged
If txtBread.Text = "" Then
lblBread.Text = Bread.ToString
lblPrice.Text = Convert.ToString(TotalPrice - Bread)
Else
Bread = Val(txtBread.Text) * 3.25
lblBread.Text = Bread.ToString
lblPrice.Text = Convert.ToString(TotalPrice + Bread)
End If
End Sub

Try with my sample ..
Public Class Form1
Dim Bread As Double
Dim TotalPrice As Double = 100 '---> maybe this is a result from a function
Private Sub txtBread_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBread.TextChanged
If txtBread.Text = "" Then
Bread = 0
Else
Bread = Val(txtBread.Text) * 3.25
End If
lblBread.Text = Bread.ToString
lblPrice.Text = (TotalPrice + Bread).ToString
End Sub
End Class

Related

Updating the current datagridview instead of adding another in VB.net

Hello sorry for asking too many questions im just really a beginner on VB.net but what I wanted to do know is instead of adding another rows how can I update that instead? like Row = Bulgogi,1,45 and then when I click on the picturebox again it will be Row = Bulgogi,2,90 and not adding Bulgogi, 1 ,45 and Bulgogi, 2 ,90 also the Quantity isnt working just stuck in 1 everytime it clicks but the increment is working fine :/
Private BibimbapQuantity = 0
Private BulgogiQuantity = 0
Private BibimbapPrice As Integer
Private BulgogiPrice As Integer
Private TotalPriceInt As Integer
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
BibimbapQuantity += 1
BibimbapPrice = 45 * BibimbapQuantity
Me.DataGridView2.Rows.Add("Bibimbap", BibimbapQuantity, BibimbapPrice)
totalPrice.Text = BibimbapPrice + BulgogiPrice
End Sub
Private Sub buttonBulgogi_Click(sender As Object, e As EventArgs) Handles buttonBulgogi.Click
BulgogiQuantity += 1
BulgogiPrice = 50 * BulgogiQuantity
Dim Satisfy As Integer = 0
Me.DataGridView2.Rows.Add("Bulgogi", BulgogiQuantity, BibimbapPrice)
totalPrice.Text = BibimbapPrice + BulgogiPrice
End Sub
Try to add this before the Me.DataGridView2.Rows.Add
Me.DataGridView2.Rows.Clear()
See the comments in the AddItemToCart() method for explanations.
Public Class Form1
Dim bibimbapQuantity, bulgogiQuantity, kimchiQuantity As Integer
Dim bibimbapTotalPrice, bulgogiTotalPrice, kimchiTotalPrice As Integer
Const bibimbapPrice As Integer = 45, bulgogiPrice As Integer = 50, kimchiPrice As Integer = 35
Private Sub buttonBibimbap_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
bibimbapQuantity += 1
bibimbapTotalPrice = bibimbapPrice * bibimbapQuantity
AddItemToCart("Bibimbap", bibimbapQuantity, bibimbapTotalPrice)
End Sub
Private Sub buttonBulgogi_Click(sender As Object, e As EventArgs) Handles buttonBulgogi.Click
bulgogiQuantity += 1
bulgogiTotalPrice = bulgogiPrice * bulgogiQuantity
AddItemToCart("Bulgogi", bulgogiQuantity, bulgogiTotalPrice)
End Sub
Private Sub buttonKimchi_Click(sender As Object, e As EventArgs) Handles buttonKimchi.Click
kimchiQuantity += 1
kimchiTotalPrice = kimchiPrice * kimchiQuantity
AddItemToCart("Kimchi", kimchiQuantity, kimchiTotalPrice)
End Sub
Sub AddItemToCart(foodName As String, quantity As Integer, totalPrice As Integer)
Dim foundFood As Boolean = False ' initially the food name is not found yet
For i = 0 To DataGridView1.Rows.Count - 1 ' loop each row in grid
If DataGridView1(0, i).Value = foodName Then ' if current row is the food you're adding
foundFood = True ' food is found
DataGridView1(1, i).Value = quantity ' set new quantity
DataGridView1(2, i).Value = totalPrice ' set new total price
Exit For ' don't check the rest of the rows because it has been found
End If
Next
If Not foundFood Then ' if food is not found in the grid
DataGridView1.Rows.Add(foodName, quantity, totalPrice) ' add a new line in the grid
End If
textboxTotalPrice.Text = bibimbapTotalPrice + bulgogiTotalPrice + kimchiTotalPrice ' calculate total for all foods
End Sub
End Class

When someone types in a word with 2-20 characters in into txtusername I want progressbar1 to =100

I can make the progress bar equal 100 if someone enters a specific word into txtUsername by writing:
Dim word1 As String = "14TSmith"
If txtUsername = word1 Then
progressbar1.value = 100
End If
(sorry I can't copy and paste the exact code because it is on another system)
But I want the user to type in any string that is between 2-20 characters long, not just a specific word.
If txtusername.TextLength >= 2 And txtusername.TextLength <= 20 Then
ProgressBar1.Value = 100
End If
I hope this helps.
If you want to give the user feedback of how "good" their password is based on the length, then:
Public Class Form1
Private Const MinLength As Integer = 2
Private Const MaxLength As Integer = 20
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.MaxLength = MaxLength
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim Percent As Double
If TextBox1.TextLength < MinLength Then
Percent = 0
Else
Percent = CDbl(TextBox1.TextLength) / CDbl(MaxLength)
End If
Dim value As Integer = ProgressBar1.Minimum + (ProgressBar1.Maximum - ProgressBar1.Minimum) * Percent
ProgressBar1.Value = Math.Max(Math.Min(value, ProgressBar1.Maximum), ProgressBar1.Minimum)
End Sub
End Class

Performing Calculations with Modules in Visual Basic

I am writing a program for my Visual Basic class that is supposed to be able to calculate the final total price for items selected from one list box and added to another. 2 of the items have sales tax that must be added into the final price. The program also has a Module that is supposed to be used to keep record of all taxes and is used to do all tax-related functions.
Here is the most current code.
Option Strict On
Public Class Form1
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
If (txtQuantity.Text = "") Then
MessageBox.Show("Please enter a quantity for the item you selected")
ElseIf Not Integer.TryParse(txtQuantity.Text, CInt(txtQuantity.Text)) Then
MessageBox.Show("The quantity entered is not numeric. Please add a numeric quantity.")
Exit Sub
Else
lstPurchased.Items.Add(txtQuantity.Text & " " & lstSale.Text)
End If
End Sub
Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculate.Click
Dim int As Integer
Dim total As Double = 0
For i As Integer = 0 To lstPurchased.Items.Count - 1
Dim lst() As String = lstPurchased.Items(i).ToString.Split({CChar(" ")}, 2)
Integer.TryParse(lst(0), int)
total += TaxesModule.SalesTax(int, lst(1))
Next
MessageBox.Show(CStr(FormatCurrency(total)))
End Sub
Private Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click
lstPurchased.Items.Clear()
txtQuantity.Clear()
End Sub
End Class
And the most recent code for the module
Option Strict On
Module TaxesModule
Private Const DONUT_TAX As Decimal = CDec(0.083)
Private Const RAISIN_TAX As Decimal = CDec(0.02)
Private Const SS_TAX As Decimal = CDec(0.062) ' <-- you are not using this
Public Function SalesTax(ByVal Quantity As Integer, ByVal item As String) As Double
Dim TotalWithSalesTax As Double
If item = "Wheat Bread" Then
TotalWithSalesTax += (Quantity * 1.15)
ElseIf item = "White Bread" Then
TotalWithSalesTax += (Quantity * 1.05)
ElseIf item = "Donuts" Then
TotalWithSalesTax += (Quantity * (0.5 * DONUT_TAX) + (Quantity * 0.5))
ElseIf item = "Raisins" Then
TotalWithSalesTax += (Quantity * (0.25 * RAISIN_TAX) + (Quantity * 0.25))
End If
Return TotalWithSalesTax
End Function
End Module
As the code is written now, the only problem I'm having is that the TotalWithSalesTax for "Raisins" is not calculating correctly. For example, if I select "Raisins" from the list box and add it with a quantity of 1 to the other list box, the total that is displayed in the message box is $0.00.
I'm starting to think the issue is with the following section of code:
For i As Integer = 0 To lstPurchased.Items.Count - 1
Dim lst() As String = lstPurchased.Items(i).ToString.Split({CChar(" ")}, 2)
Because I have tried making changes such as
For i As Integer = 1 ...
And that caused both Donuts and Raisins to give me a total of $0.00 in the message box. So I'm wondering if maybe how the count is set up is not allowing it to go through the entire list, but I don't know how to fix that?
I'm still really new to Visual Basic, and programming in general really and this is my first time working with modules. Can anybody help me figure out what I'm doing wrong, so I can move onto other parts of the program?
I have modified your code, hope this is what you want. Added comment in the code. Note i have removed all Public variables.
Public Class Form1
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
If (txtQuantity.Text = "") Then
MessageBox.Show("Please enter a quantity for the item you selected")
ElseIf Not Integer.TryParse(txtQuantity.Text, txtQuantity.Text) Then
MessageBox.Show("The quantity entered is not numeric. Please add a numeric quantity.")
Exit Sub
Else
lstPurchased.Items.Add(txtQuantity.Text & " " & lstSale.Text)
End If
End Sub
Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculate.Click
Dim int As Integer
Dim total As Double = 0
'you have to loop through the list to calculate the total
' hope this is what you want
For i As Integer = 0 To lstPurchased.Items.Count - 1
' We split the list item into the quantity and the item name
Dim lst() As String = lstPurchased.Items(i).ToString.Split({CChar(" ")}, 2)
'lst(1) contains the item name
Integer.TryParse(lst(0), int)
total += TaxesModule.SalesTax(int, lst(1))
Next
MessageBox.Show(CStr(FormatCurrency(total)))
End Sub
Private Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click
lstPurchased.Items.Clear()
txtQuantity.Clear()
End Sub
End Class
The module
Module TaxesModule
Private Const DONUT_TAX As Decimal = CDec(0.083)
Private Const RAISIN_TAX As Decimal = CDec(0.02)
Private Const SS_TAX As Decimal = CDec(0.062) ' <-- you are not using this
Public Function SalesTax(ByVal Quantity As Integer, ByVal item As String) As Double
Dim TotalWithSalesTax As Double
If item = "Wheat Bread" Then
TotalWithSalesTax = (Quantity * 1.15)
ElseIf item = "White Bread" Then
TotalWithSalesTax = (Quantity * 1.05)
ElseIf item = "Donuts" Then
TotalWithSalesTax = (Quantity * (0.5 * DONUT_TAX) + (Quantity * 0.5))
ElseIf item = "Raisins" Then
TotalWithSalesTax = (Quantity * (0.25 * RAISIN_TAX) + (Quantity * 0.25))
End If
Return TotalWithSalesTax
End Function
End Module

Run Random event once?

The code below runs and keeps repeating over and over again. I only pasted the up_timer but others do the same. Any ideas on how to make it run once, then repeat to the random loop and so on?
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Application.DoEvents()
Randomize()
Dim value As Integer = CInt(Int((4 * Rnd()) + 1))
If value = 1 Then
MsgBox("Up")
up.Start()
ElseIf value = 2 Then
MsgBox("Down")
down.Start()
ElseIf value = 3 Then
MsgBox("Left")
left.Start()
ElseIf value = 4 Then
MsgBox("Right")
right.Start()
End If
Timer1.Stop()
End Sub
Private Sub up_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles up.Tick
Static moveCount As Integer = 1
If Me.mob2.Location.Y > 12 Then
Me.mob2.Location = New Point(Me.mob2.Location.X, Me.mob2.Location.Y - 5)
End If
moveCount += 1
If moveCount = 10 Then
moveCount = 1
Me.Timer1.Start()
Me.up.Stop()
End If
End Sub
Looks like this is what you need, based on your complaint that timer1 repeats same numbers
Dim rand1 As New Random(CInt(Date.Now.Ticks And &h0000FFFF))
Dim value As Integer = rand1.Next(1, 4)
This will not repeat
Plus, you have to move Timer1.Stop() to beginning of the method

The button.BackColor statement seem to be executed, but no colour change is show

I'm trying to re-create the classic game "Simon" for a project. The code I have here should hopefully create a random number, translate that to a colour change for a random button, wait a short time, and then do the same for another random button. I can't spot any problems, but on execution the buttons remain uchanged.
Public Class MenuForm
Dim failure As Boolean
Dim pattern() As Integer
Dim maincounter As Integer = 1
Dim diff As Integer
Dim sender As Object
Dim e As EventArgs
Dim timewaited As Integer
Dim timefinished As Boolean
Private Sub Menuform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Get the difficulty level from the player
Dim InputtedDifficulty As Integer = InputBox("Please enter difficulty. 1-Easy 2-Medium 3-Hard")
'Validate difficulty choice
Do While InputtedDifficulty > 3 Or InputtedDifficulty < 1
InputtedDifficulty = InputBox("Input incorrect. Please re-enter selection. 1-Easy 2-Medium 3-Hard")
Loop
'Set speed of blinking based on difficulty choice
Select Case InputtedDifficulty
Case 1
diff = 1000
Case 2
diff = 500
Case 3
diff = 20
End Select
End Sub
Private Sub run_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles run.Click
Call GameController()
End Sub
Private Sub GameController()
Dim buttonRepeater As Integer
'Call checkFail()
Do While failure = False
maincounter = maincounter + 1
Call Pattern_creator(sender, e)
For buttonRepeater = 1 To maincounter
Call button_controller(sender, e)
timewaited = 0
timefinished = False
ButtonTimer.Enabled = True
If timefinished = True Then
End If
Button1.BackColor = Color.Blue
Button2.BackColor = Color.Blue
Button3.BackColor = Color.Blue
Button4.BackColor = Color.Blue
Next buttonRepeater
Loop
End Sub
Private Sub Pattern_creator(ByVal sender As System.Object, ByVal e As System.EventArgs)
ReDim Preserve pattern(maincounter)
Randomize()
pattern(maincounter) = Int((Rnd() * 4) + 1)
ReDim Preserve pattern(maincounter + 1)
End Sub
Private Sub button_controller(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Ths case statement takes the random number generated earlier and translates that to
'a button flash
Select Case pattern(maincounter)
Case 1
Button1.BackColor = Color.Red
Case 2
Button2.BackColor = Color.Red
Case 3
Button3.BackColor = Color.Red
Case 4
Button4.BackColor = Color.Red
End Select
End Sub
Private Sub ButtonTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTimer.Tick
If timewaited = 5 Then
ButtonTimer.Enabled = False
timefinished = True
Else
timewaited = timewaited + 1
End If
End Sub
End Class
Any help would be very much appreciated, I've been staring at this for ages with no progress.