Results 1 to 9 of 9

Thread: [RESOLVED] How do I Automatically update a text box, using a NumbericUpDown Box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Resolved [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?
    Last edited by dday9; Jan 13th, 2021 at 10:22 PM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    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.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    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?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Re: How do I Automatically update a text box, using a NumbericUpDown Box

    Thank you, I'll take a look.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: How do I Automatically update a text box, using a NumbericUpDown Box

    Quote Originally Posted by dday9 View Post
    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 View Post
    Thank you, I'll take a look.
    You can combine that with what i told you in your other thread, about multiple event handlers

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: How do I Automatically update a text box, using a NumbericUpDown Box

    Quote Originally Posted by .paul. View Post
    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: How do I Automatically update a text box, using a NumbericUpDown Box

    Quote Originally Posted by dday9 View Post
    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

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: [RESOLVED] How do I Automatically update a text box, using a NumbericUpDown Box

    Ah I see, I misunderstood.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width