How to add/minus multiple checkbox values - vb.net

I'm using VB.net and I'm having trouble trying to add different values when I select checkboxes and diplaying the total in a textbox. When unchecking them, it should be subtracting the values. Here is my code
"totals" is my textbox
Dim total As Double
Private Sub cchstk_CheckStateChanged(sender As Object, e As EventArgs) Handles cchstk.CheckStateChanged
If (cchstk.Checked = True) Then
total = total + 109.99
totals.Text = Double.Parse(total)
ElseIf (chstk.Checked = False) Then
total = total - 109.99
totals.Text = Double.Parse(total)
End If
End Sub
Private Sub cms_CheckStateChanged(sender As Object, e As EventArgs) Handles cms.CheckStateChanged
If (cms.Checked = True) Then
total = total + 79.99
totals.Text = Double.Parse(total)
ElseIf (chstk.Checked = False) Then
total = total - 79.99
totals.Text = Double.Parse(total)
End If
End Sub
Private Sub prrdg_CheckStateChanged(sender As Object, e As EventArgs) Handles prrdg.CheckStateChanged
If (prrdg.Checked = True) Then
total = total + 49.99
totals.Text = Double.Parse(total)
ElseIf (chstk.Checked = False) Then
total = total - 49.99
totals.Text = Double.Parse(total)
End If
End Sub
Private Sub gb_CheckStateChanged(sender As Object, e As EventArgs) Handles gb.CheckStateChanged
If (gb.Checked = True) Then
total = total + 29.99
totals.Text = Double.Parse(total)
ElseIf (chstk.Checked = False) Then
total = total - 29.99
totals.Text = Double.Parse(total)
End If
End Sub
Private Sub nd_CheckStateChanged(sender As Object, e As EventArgs) Handles nd.CheckStateChanged
If (nd.Checked = True) Then
total = total + 29.99
totals.Text = Double.Parse(total)
ElseIf (chstk.Checked = False) Then
total = total - 29.99
totals.Text = Double.Parse(total)
End If
End Sub`

you only have to change all your Double.Parse(total) to total.ToString
you want to convert a number to string, not a number to a number.
also do not use double if you can, you will be hit by floating conversion issue
use decimal and you can also create a method to reduce the repetition of code
Dim total As Decimal
Private Sub ChangeValue(add As Boolean, value As Decimal)
total += If(add, value, -value)
TextBox1.Text = total.ToString
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
ChangeValue(DirectCast(sender, CheckBox).Checked, 109.99D)
End Sub
Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
ChangeValue(DirectCast(sender, CheckBox).Checked, 79.99D)
End Sub
Private Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox3.CheckedChanged
ChangeValue(DirectCast(sender, CheckBox).Checked, 49.99D)
End Sub
Private Sub CheckBox4_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox4.CheckedChanged
ChangeValue(DirectCast(sender, CheckBox).Checked, 29.99D)
End Sub
Private Sub CheckBox5_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox5.CheckedChanged
ChangeValue(DirectCast(sender, CheckBox).Checked, 29.99D)
End Sub

Related

How to adjust the price when I remove an item in my DataGridView (Does not connect with any database) Vb.net

I am trying to adjust the subtotal when i remove the item in my data grid view (does not connect any data base.
`
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.DataGridView1.Rows.Add("H.Burger", "10.90")
Subtotal = Subtotal + "RM10.9"
Me.Label13.Text = Subtotal.ToString()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.DataGridView1.Rows.Add("S.Carborana", "12.90")
Subtotal = Subtotal + 12.9
Me.Label13.Text = Subtotal.ToString()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Me.DataGridView1.Rows.Add("N.Lemak", "5.90")
Subtotal = Subtotal + 5.9
Me.Label13.Text = Subtotal.ToString()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Me.DataGridView1.Rows.Add("CurryMee", "7.90")
Subtotal = Subtotal + 7.9
Me.Label13.Text = Subtotal.ToString()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Me.DataGridView1.Rows.Add("CCRice", "8.90")
Subtotal = Subtotal + 8.9
Me.Label13.Text = Subtotal.ToString()
End Sub
You can try like below code
Private Sub AdjustPrice()
Dim price As Decimal = 0
For x = 0 To Datagridview1.Rows.Count - 1
If Not IsDBNull(Datagridview1.Rows(x).Cells([your cell goes here]).Value) Then price = price + Datagridview1.Rows(x).Cells([your cell goes here]).Value
Next
lblPrice.Text = FormatCurrency(sum.ToString())
End Sub
set the the AdjustPrice on form load or where you want to start the action.
from above code, you can display your Price on Lable or Textbox
Try and let me know

Multiplication application, visual basic. invalid cast exception

Public Class Form1
Dim randomNumberOne As New Random
Dim randomNumberTwo As New Random
Dim ChildGrade As Integer
Dim strChildGrade As Integer
Dim strcorrectCounter As Integer
Dim Correctcounter As Integer = 0
Dim WrongCounter As Integer = 0
Dim totalQuestions As Integer = 0
Dim percent As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ChildGrade = InputBox("Enter your age ", "age")
grade()
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberOne.Next(0, 12)
End Sub
Private Sub grade()
If ChildGrade <= 4 Then
Gradelbl.Text = "Preschool"
ElseIf ChildGrade = 5 Then
Gradelbl.Text = "Kindergarden"
ElseIf ChildGrade <= 10 Then
Gradelbl.Text = "Elementary"
ElseIf ChildGrade <= 13 Then
Gradelbl.Text = "Middle School"
ElseIf ChildGrade <= 18 Then
Gradelbl.Text = "High School"
ElseIf ChildGrade > 18 Then
Gradelbl.Text = "NA"
End If
End Sub
Private Sub RandomNumberToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RandomNumberToolStripMenuItem.Click
NumbersGroupBox.Enabled = False
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
End Sub
Private Sub SingleTableToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SingleTableToolStripMenuItem.Click
NumbersGroupBox.Enabled = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FirstNumberlbl.Text = "1"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
FirstNumberlbl.Text = "2"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
FirstNumberlbl.Text = "3"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
FirstNumberlbl.Text = "4"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
FirstNumberlbl.Text = "5"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
FirstNumberlbl.Text = "6"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
FirstNumberlbl.Text = "7"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
FirstNumberlbl.Text = "8"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
FirstNumberlbl.Text = "9"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
FirstNumberlbl.Text = "10"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
FirstNumberlbl.Text = "11"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
FirstNumberlbl.Text = "12"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub CheckAnswer()
If TextBox1.Text <> (FirstNumberlbl.Text * SecondNumberlbl.Text) Then
MsgBox("Please try again", MsgBoxStyle.Information, "Answer")
WrongCounter = WrongCounter + 1
If WrongCounter = 2 Then
MsgBox("The correct answer is " & FirstNumberlbl.Text * SecondNumberlbl.Text, MsgBoxStyle.Information, "Correct answer")
WrongCounter = 0
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
End If
TextBox1.Text = ""
End If
End Sub
Private Sub rightAnswer()
If FirstNumberlbl.Text * SecondNumberlbl.Text = TextBox1.Text Then
Correctcounter = Correctcounter + 1
NumberCorrectlbl.Text = Correctcounter
TextBox1.Text = ""
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
End If
totalQuestions = totalQuestions + 1
End Sub
Private Sub Enterbtn_Click(sender As Object, e As EventArgs) Handles Enterbtn.Click
CheckAnswer()
rightAnswer()
TotalCountlbl.Text = totalQuestions
PercentCorrectlbl.Text = ((Correctcounter * 100) / (totalQuestions * 100)) * 100
End Sub
End Class
I'm creating a multiplication application in VB that keeps count of how many questions the user gets right and how many they get wrong. When I run the program, it works fine in counting the amount right and totaling up the percentage, but when I test for when they get it wrong I get this error. enter image description here
if the image isn't visible, the error says,
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "" to type 'Double' is not valid
The program points to the problem being in the if statement in the rightAnswer() sub class but i'm not exactly sure what is wrong with it.
The problem is you are trying to do calculations with strings. Strings are text, not numbers.
This is not a full answer but enough to get you to understand the problem and change your code to fix it.
Don't do any calculations on the contents of text boxes and/or labels. Use the correct variables for such tasks. Use the controls for simply retrieving and displaying. This will save you a lot of heart-ache in the long run.
So for example, don't do:
If TextBox1.Text <> (FirstNumberlbl.Text * SecondNumberlbl.Text) Then
You're checking a number in the TextBox to a calculation based on two labels, they are all text!
So think along the lines of doing calculations on a variable fit for purpose, an integer for example:
Example:
Dim FirstNumber As Integer
Dim SecondNumber As Integer
Use these for the calculations, then, display the result as text.
If you are expecting a floating point number, a decimal, change the variable type as needed.
In your 'CheckAnswer' subroutine you should starting thinking along the lines of:
If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then
'We have something in the TextBox (you can check to allow only numbers to be entered in the TextBox.KeyDown/Press event)
If Not Integer.Parse(TextBox1.Text) = (FirstNumber * SecondNumber) Then
'Do what you need ...
End If
End If
There's a couple of other things too that could simplify your code.
You don't need 12 button events all doing the same thing.
In you Form1_Load Event put this:
Dim bTags() As Button = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9, Button10, Button11, Button12}
For t As Integer = 0 To bTags.Count - 1
bTags(t).Tag = t + 1
Next
Then delete all your 12 Button Click events and replace them with:
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button12.Click, Button11.Click, Button10.Click, Button1.Click
Dim selectedButton As Button = CType(sender, Button)
FirstNumberlbl.Text = selectedButton.Tag.ToString
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Job done...
Also, you grade subroutine would be better of as a Select Case function:
In your Form1_Load replace: grade() with Gradelbl.Text = Grade() and your grade() subroutine replaced with:
Private Function Grade() As String
Select Case ChildGrade
Case < 5 : Return "Preschool"
Case = 5 : Return "Kindergarden"
Case < 11 : Return "Elementary"
Case < 14 : Return "Middle School"
Case < 19 : Return "High School"
Case Else : Return "N/A"
End Select
End Function
I hope some of the advice will be of help.
What you need to do is cast the values of the textbox. For example:
Convert.toInt32(FirstNumberlbl.Text)
or
Dim firstNumber as Integer
Integer.TryParse(FirstNumberlbl.Text, firstNumber)
Or assign a variable to the textbox and cast the variable instead of the textbox.

calculation is only performed once on food ordering form (VB)

I am making a food ordering form, and I am having trouble with the calculations. Every time a hotdog is ordered the form is supposed to add the cost to the total amount, so one hotdog is 2 dollars, add another and it says 4 dollars, etc. But it only performs the calculation once, so no matter how many items I add to the order the price stays the same, as if I had only ordered one.
I don't get any errors at runtime so it must be a logical error. Here is the full code. I don't know what other information I should give at this moment. All help is appreciated.
Public Class DroneDogs
Dim DogChoice As String
Dim condiment1 As String
Dim condiment2 As String
Dim condiment3 As String
Const DBL_TAX_RATE As Double = 0.07
Const DBL_DRONE_DOG As Double = 1.99
Dim intNumDog As Integer
Dim dblSubTotal As Double
Dim dblSalesTax As Double
Dim dblTotalCost As Double
Private Sub btnAddCustomer_Click(sender As Object, e As EventArgs) Handles btnAddCustomer.Click
cboCurrentCustomers.Items.Add(txtAddCustomer.Text)
End Sub
Private Sub optBeef_CheckedChanged(sender As Object, e As EventArgs) Handles optBeef.CheckedChanged
If (optBeef.Checked) Then
DogChoice = "Beef Dog"
End If
End Sub
Private Sub optPork_CheckedChanged(sender As Object, e As EventArgs) Handles optPork.CheckedChanged
If (optPork.Checked) Then
DogChoice = "Pork Dog"
End If
End Sub
Private Sub optTurkey_CheckedChanged(sender As Object, e As EventArgs) Handles optTurkey.CheckedChanged
If (optTurkey.Checked) Then
DogChoice = "Turkey Dog"
End If
End Sub
Private Sub chkKetchup_CheckedChanged(sender As Object, e As EventArgs) Handles chkKetchup.CheckedChanged
If (chkKetchup.Checked) Then
condiment1 = " + Ketchup"
Else
condiment1 = Nothing
End If
End Sub
Private Sub chkMustard_CheckedChanged(sender As Object, e As EventArgs) Handles chkMustard.CheckedChanged
If (chkMustard.Checked) Then
condiment2 = " + Mustard"
Else
condiment2 = Nothing
End If
End Sub
Private Sub chkRelish_CheckedChanged(sender As Object, e As EventArgs) Handles chkRelish.CheckedChanged
If (chkRelish.Checked) Then
condiment3 = " + Relish"
Else
condiment3 = Nothing
End If
End Sub
Private Sub btnAddDog_Click(sender As Object, e As EventArgs) Handles btnAddDog.Click
intNumDog = +1
dblSubTotal = intNumDog * DBL_DRONE_DOG
dblSalesTax = dblSubTotal * DBL_TAX_RATE
dblTotalCost = dblSubTotal + dblSalesTax
txtSub.Text = dblSubTotal.ToString("c2")
txtTax.Text = dblSalesTax.ToString("c2")
txtTotal.Text = dblTotalCost.ToString("c2")
Dim addCondiment As String = condiment1 + condiment2 + condiment3
If (DogChoice = "Beef Dog") Or
(DogChoice = "Pork Dog") Or
(DogChoice = "Turkey Dog") Then
lstOrder.Items.Add(DogChoice + addCondiment + ": " + txtTotal.Text)
Else
MsgBox("Please select a Hot Dog type.")
End If
End Sub
Private Sub btnClearOrder_Click(sender As Object, e As EventArgs) Handles btnClearOrder.Click
lstOrder.Items.Clear()
txtSub.Clear()
txtTax.Clear()
txtTotal.Clear()
chkKetchup.Checked = 0
chkMustard.Checked = 0
chkRelish.Checked = 0
optBeef.Checked = 0
optPork.Checked = 0
optTurkey.Checked = 0
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Exits the program
If MessageBox.Show("Do you want to exit the DroneDogs application?", "DroneDogs",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
= DialogResult.Yes Then
Application.Exit()
End If
End Sub
Private Sub btnOrderComplete_Click(sender As Object, e As EventArgs) Handles btnOrderComplete.Click
Receipt.Show()
End Sub
Private Sub btnRemoveDog_Click(sender As Object, e As EventArgs) Handles btnRemoveDog.Click
lstOrder.Items.Remove(lstOrder.SelectedItem)
End Sub
End Class
I think the problem is with the line
intNumDog = +1
In this line, you always set intNumDog to the value of +1. This is the same as:
intNumDog = 1
You want to add one to the value of intNumDog:
intNumDog += 1

VB.Net - Noncooperative decimal in string

I am writing a calculator WinForm in VB. I have a label used to display the output "Result". My problem comes when trying to add a "." to my label string. Example: I will type 355.5 and until the 5 is pressed after it, my string is showing up as .355 After the last 5 is pressed, it jumps into the correct location. I have exhausted my debugging skill and now am just going crazy. Anyone encounter this before?
Here's my entire code so far (ignore unfinished functions)
Public Class MyCalc
Private bDecFlag As Boolean
Private sMathOp As String
Private Sub ModeSel_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ModeSel.SelectedIndexChanged
If ModeSel.SelectedIndex = 3 Then
Me.Width = 360
ElseIf ModeSel.SelectedIndex > 2 Then
Me.Width = 590
Else
Me.Width = 250
End If
If ModeSel.SelectedIndex = 0 Then
For iCount As Integer = 0 To 9
Me.Controls("Digit" & iCount).Enabled = True
Next
End If
If ModeSel.SelectedIndex = 1 Then
For iCount As Integer = 2 To 9
Me.Controls("Digit" & iCount).Enabled = False
Next
End If
If ModeSel.SelectedIndex = 2 Then
For iCount As Integer = 0 To 7
Me.Controls("Digit" & iCount).Enabled = True
Next
Digit8.Enabled = False
Digit9.Enabled = False
End If
If ModeSel.SelectedIndex = 3 Then
For iCount As Integer = 0 To 9
Me.Controls("Digit" & iCount).Enabled = True
Next
For iCount As Integer = Asc("A") To Asc("F")
Me.Controls("Alpha" & Chr(iCount)).Enabled = True
Next
For iCount As Integer = Asc("G") To Asc("Z")
Me.Controls("Alpha" & Chr(iCount)).Enabled = False
Next
End If
If ModeSel.SelectedIndex = 4 Then
For iCount As Integer = 0 To 9
Me.Controls("Digit" & iCount).Enabled = True
Next
For iCount As Integer = Asc("A") To Asc("Z")
Me.Controls("Alpha" & Chr(iCount)).Enabled = True
Next
AlphaA.Enabled = False
AlphaE.Enabled = False
AlphaI.Enabled = False
AlphaO.Enabled = False
AlphaU.Enabled = False
End If
End Sub
Private Sub Digit0_Click(sender As System.Object, e As System.EventArgs) Handles Digit0.Click
UpdateResult(0)
End Sub
Private Sub Digit1_Click(sender As System.Object, e As System.EventArgs) Handles Digit1.Click
UpdateResult(1)
End Sub
Private Sub Digit2_Click(sender As System.Object, e As System.EventArgs) Handles Digit2.Click
UpdateResult(2)
End Sub
Private Sub Digit3_Click(sender As System.Object, e As System.EventArgs) Handles Digit3.Click
UpdateResult(3)
End Sub
Private Sub Digit4_Click(sender As System.Object, e As System.EventArgs) Handles Digit4.Click
UpdateResult(4)
End Sub
Private Sub Digit5_Click(sender As System.Object, e As System.EventArgs) Handles Digit5.Click
UpdateResult(5)
End Sub
Private Sub Digit6_Click(sender As System.Object, e As System.EventArgs) Handles Digit6.Click
UpdateResult(6)
End Sub
Private Sub Digit7_Click(sender As System.Object, e As System.EventArgs) Handles Digit7.Click
UpdateResult(7)
End Sub
Private Sub Digit8_Click(sender As System.Object, e As System.EventArgs) Handles Digit8.Click
UpdateResult(8)
End Sub
Private Sub Digit9_Click(sender As System.Object, e As System.EventArgs) Handles Digit9.Click
UpdateResult(9)
End Sub
Private Sub DecBut_Click(sender As System.Object, e As System.EventArgs) Handles DecBut.Click
If bDecFlag = False Then
If Result.Text = "0" Then
Result.Text = "0."
bDecFlag = True
Else
Result.Text = Result.Text + "."
bDecFlag = True
End If
End If
End Sub
Private Sub ClrBut_Click(sender As System.Object, e As System.EventArgs) Handles ClrBut.Click
Result.Text = 0
bDecFlag = False
End Sub
Private Sub DelBut_Click(sender As System.Object, e As System.EventArgs) Handles DelBut.Click
If Result.Text = "0" Then
ElseIf Result.Text.Substring(Result.Text.Length - 1) = "." Then
Result.Text = Result.Text.Substring(0, Result.Text.Length - 1)
bDecFlag = False
Else
Result.Text = Result.Text.Substring(0, Result.Text.Length - 1)
If Result.Text = "" Then
Result.Text = "0"
End If
End If
End Sub
Private Sub MyCalc_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.NumPad0
Digit0_Click(Digit0, New EventArgs)
Case Keys.NumPad1
Digit1_Click(Digit1, New EventArgs)
Case Keys.NumPad2
Digit2_Click(Digit2, New EventArgs)
Case Keys.NumPad3
Digit3_Click(Digit3, New EventArgs)
Case Keys.NumPad4
Digit4_Click(Digit4, New EventArgs)
Case Keys.NumPad5
Digit5_Click(Digit5, New EventArgs)
Case Keys.NumPad6
Digit6_Click(Digit6, New EventArgs)
Case Keys.NumPad7
Digit7_Click(Digit7, New EventArgs)
Case Keys.NumPad8
Digit8_Click(Digit8, New EventArgs)
Case Keys.NumPad9
Digit9_Click(Digit9, New EventArgs)
Case Keys.Back
DelBut_Click(DelBut, New EventArgs)
Case Keys.Decimal
DecBut_Click(DecBut, New EventArgs)
End Select
End Sub
Private Sub MyCalc_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
bDecFlag = False
End Sub
Public Sub UpdateResult(ByVal sNum As String)
If Result.Text = "0" Then
Result.Text = sNum
Else
Result.Text &= sNum
End If
End Sub
End Class
So after the long debugging, I found out that you are using the Label/Textbox RightToLeft property set to yes. Nothing is wrong with it really, but it can't handle special characters including decimal points, commas, etc. properly. The workaround for this is to reverse what we have expected - to reverse the string concatenation.
Result.Text = "." & Result.Text
Not seeing entire code I'm not exactly sure but I think there might be something wrong with string concatenation.
Try changing into this:
Result.Text = Result.Text & "."

VB.net real-time time elapsed feature

I already have created a real time clock that synchronizes with the computer time and is being displayed in a label.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
I want to make a real-time time elapsed feature that keeps on counting the seconds/minutes/hours elapsed from the time it started till the time it stops and it would be basing on the real-time clock i have created. I would be creating a start and stop button for this. Is this possible? Thanks in advance.
I am now able to complete everything and i added a feature that records the starting and ending time based on my real time clock. Here is my working code:
Dim hr, min, sec As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Start.Text = ""
EndLbl.Text = ""
Elapse.Text = ""
Timer2.Enabled = True
Start.Text = Time.Text
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
sec = sec + 1
If (sec = 60) Then
sec = 0
min = min + 1
ElseIf (min = 60) Then
min = 0
hr = hr + 1
ElseIf (hr = 24) Then
hr = 0
min = 0
sec = 0
End If
Elapse.Text = String.Format("{0}hr : {1}min : {2}sec", hr, min, sec)
Timer2.Interval = 1000
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Timer2.Enabled = False
EndLbl.Text = Label4.Text
hr = 0
min = 0
sec = 0
Timer2.Interval = 1
End Sub
Credits to the starting code given by NeverHopeless. Thanks alot.
I suggest you use only 1 timer:
Public Class Form2
Private _elapseTimerRunning As Boolean = False
Private _elapseStartTime As DateTime
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
txtTime.Text = Now.ToString("h:mm:ss tt")
If _elapseTimerRunning = True Then
Dim elapsedtime = DateTime.Now.Subtract(_elapseStartTime)
txtElapsed.Text = String.Format("{0}hr : {1}min : {2}sec", elapsedtime.Hours, elapsedtime.Minutes, elapsedtime.Seconds)
End If
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
_elapseStartTime = DateTime.Now
_elapseTimerRunning = True
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
_elapseTimerRunning = False
End Sub
End Class
An example for displaying the elapsed time the application has run.
Public Class Form1
'shows elapsed time that the app has run
'
Dim elapTimer As New Threading.Timer(AddressOf tick, Nothing, 1000, 1000)
Dim stpw As Stopwatch = Stopwatch.StartNew
Private Sub tick(state As Object)
If stpw.IsRunning Then
'format - http://msdn.microsoft.com/en-us/library/ee372287.aspx
Me.Invoke(Sub()
Label1.Text = stpw.Elapsed.ToString("d\ \ hh\:mm\:ss\.ff")
End Sub)
End If
End Sub
End Class
To add start/stop functionality using buttons:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'start the elapsed timer
stpw.Start() 'continue
'or
'stpw.Restart() 'restart
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
stpw.Stop()
End Sub
Something like this would help you: (untested, but will give you a starter)
Dim hr, min, sec As Integer 'msec;
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer2.Enabled = True
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'msec++;
'if(msec == 60) { msec = 0; sec++; }
sec+=1;
if(sec = 60) Then
sec = 0 : min+=1
end if
if(min = 60) Then
min = 0 : hr+=1
end if
if(hr = 24) Then
hr = 0 : min = 0 : sec = 0
end if
'TimeElapsed.Text = String.Format("{0}:{1}:{2} {3}", hr, min, sec, msec)
TimeElapsed.Text = String.Format("{0}:{1}:{2}", hr, min, sec)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
NOTE: Timer2 will run for every second.