[RESOLVED] How do I Automatically update a text box, using a NumbericUpDown Box
I'm trying to make my program so it auto updates the price in the text boxes for this "Pod" whenever I increase or decrease the value of the NumericUpDown box.
People.Value is the NumericUpDown Box
'Calculating and Displaying Prices for Liverpool Pod
Code:
If cboxPod.Text = "Liverpool" = True And People.Value >= 0 Then
TotalPrice.Text = "£" & (9 * People.Value)
VAT.Text = ("£" & TotalPrice.Text * 0.2)
Subtotal.Text = ("£" & TotalPrice.Text * 0.8)
End If
Does anyone have any help?
Re: How do I Automatically update a text box, using a NumbericUpDown Box
You have to call the code to update the textbox in the ValueChanged event handler of the NumericUpDown control.
That means the code to update the textbox should be put in its own method so that it can be called to update the textbox from wherever needed, such as when you change the location, or the number of people, or the number of days, etc...
Each of the places you can change one of the inputs, the changed event for that input can call the common method to update the textbox.
Re: How do I Automatically update a text box, using a NumbericUpDown Box
So there is no way I specifically can make it auto update as I will be having a ComboBox, A NumbericUpDown Value, and two DateTimePickers which will all effect the price?
Re: How do I Automatically update a text box, using a NumbericUpDown Box
There are a couple of notes:
- You have Option Strict turned off
- You can use ToString and pass the currency format
- You will need to leverage the ValueChanged event of the NumericUpDown
- You do not need the = True after you compare cboxPod's Text
- You may also want to handle the ComboBox's SelectedIndexChanged event too.
For more information on Option Strict and why it should be turned on, take a look at my website: https://www.vblessons.com/lessons.html#/1/2
For more information on formatting numbers, take a look at MSDN: https://docs.microsoft.com/en-us/dot...format-strings
For more information on ValueChanged, take a look at MSDN: https://docs.microsoft.com/en-us/dot...n.valuechanged
The reason why you do not need the = True is because you are already checking if the text equals a value is true. Quick side note, if the Minimum property of the NumericUpDown is 0 then you won't need to check if it is greater than or equal to 0.
The reason why you may want to handle the ComboBox's SelectedIndexChanged event is what if you start to handle multiple values in the ComboBox? Wouldn't you want the prices to update when you selected a new item?
Take a look at this code:
Code:
Private Sub ComboBoxOrNumericUpDown_Changed(sender As Object, e As EventArgs) Handles People.ValueChanged, cboxPod.SelectedIndexChanged
If cboxPod.Text = "Liverpool" Then
Dim totalPriceValue = 9 * People.Value
Dim vatValue = totalPriceValue * 0.2 'theft
Dim subTotalValue = totalPriceValue * 0.8
TotalPrice.Text = totalPriceValue.ToString("C")
VAT.Text = vatValue.ToString("C")
Subtotal.Text = subTotalValue.ToString("C")
End If
End Sub
Re: How do I Automatically update a text box, using a NumbericUpDown Box
Thank you, I'll take a look.
Re: How do I Automatically update a text box, using a NumbericUpDown Box
Quote:
Originally Posted by
dday9
There are a couple of notes:
- You have Option Strict turned off
- You can use ToString and pass the currency format
- You will need to leverage the ValueChanged event of the NumericUpDown
- You do not need the = True after you compare cboxPod's Text
- You may also want to handle the ComboBox's SelectedIndexChanged event too.
For more information on Option Strict and why it should be turned on, take a look at my website:
https://www.vblessons.com/lessons.html#/1/2
For more information on formatting numbers, take a look at MSDN:
https://docs.microsoft.com/en-us/dot...format-strings
For more information on ValueChanged, take a look at MSDN:
https://docs.microsoft.com/en-us/dot...n.valuechanged
The reason why you do not need the
= True is because you are already checking if the text equals a value is true. Quick side note, if the Minimum property of the NumericUpDown is 0 then you won't need to check if it is greater than or equal to 0.
The reason why you may want to handle the ComboBox's SelectedIndexChanged event is what if you start to handle multiple values in the ComboBox? Wouldn't you want the prices to update when you selected a new item?
Take a look at this code:
Code:
Private Sub ComboBoxOrNumericUpDown_Changed(sender As Object, e As EventArgs) Handles People.ValueChanged, cboxPod.SelectedIndexChanged
If cboxPod.Text = "Liverpool" Then
Dim totalPriceValue = 9 * People.Value
Dim vatValue = totalPriceValue * 0.2 'theft
Dim subTotalValue = totalPriceValue * 0.8
TotalPrice.Text = totalPriceValue.ToString("C")
VAT.Text = vatValue.ToString("C")
Subtotal.Text = subTotalValue.ToString("C")
End If
End Sub
Quote:
Originally Posted by
THopwood
Thank you, I'll take a look.
You can combine that with what i told you in your other thread, about multiple event handlers
Re: How do I Automatically update a text box, using a NumbericUpDown Box
Quote:
Originally Posted by
.paul.
You can combine that with what i told you in your other thread, about multiple event handlers
I don't know if you saw or not, but that is what I did in my example.
Re: How do I Automatically update a text box, using a NumbericUpDown Box
Quote:
Originally Posted by
dday9
I don't know if you saw or not, but that is what I did in my example.
I saw what you did. The other thread was a slightly different question
Re: [RESOLVED] How do I Automatically update a text box, using a NumbericUpDown Box
Ah I see, I misunderstood.