none
How to show the value of sum in Textbox without click Button RRS feed

  • Question

  • Hi guys, i would like to ask more again.

    I have two Textbox and one Combobox. Textbox name's are txStay and txPay, name's of Combobox is txChoice

    Inside Combobox there's a lot of case that when I click one of them, it will show the value like this :

            Select Case cbChoice.SelectedItem
                Case "VIP"
                    txPay.Text = "750,000"
                Case "Kelas I"
                    txPay.Text = "500,000"
                Case "Kelas II"
                    txPay.Text = "300,000"
                Case "Kelas III"
                    txPay.Text = "150,000"
            End Select

    Then when I add value of textbox 'txStay' as Integer, I want to show the value of "txStay * txChoice" inside 'txPay'

    That txChoice and txPay read as Double so I can format as Double

    Any result, please? I'm so appreciate that...

    Thanks

    Regards,

    Aga

    Sunday, April 1, 2018 1:41 PM

All replies

  • Hi

    Very confusing.

    You are heading for a bad bit of coding where you are setting a textbox text with a value based on itself and another textbox value.

    Here is what you say you want, but trouble ahead.

    ' Form1 with txChoice, txPay and txStay
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim temp As Double = 0D
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With txChoice
          .Items.Add("VIP")
          .Items.Add("Kelas I")
          .Items.Add("Kelas II")
          .Items.Add("Kelas III")
        End With
      End Sub
      Private Sub txChoice_SelectedIndexChanged(sender As Object, e As EventArgs) Handles txChoice.SelectedIndexChanged
        Dim cb As ComboBox = DirectCast(sender, ComboBox)
        Select Case cb.SelectedItem.ToString
          Case "VIP"
            txPay.Text = "750,000"
          Case "Kelas I"
            txPay.Text = "500,000"
          Case "Kelas II"
            txPay.Text = "300,000"
          Case "Kelas III"
            txPay.Text = "150,000"
        End Select
      End Sub
      Function Getdouble(s As String) As Double
        Dim v As Double = 0D
        If Double.TryParse(s, v) Then Return v
        Return 0D
      End Function
      Private Sub txStay_TextChanged(sender As Object, e As EventArgs) Handles txStay.Validated
        ' first, store the value before overwriting it.
        temp = Getdouble(txPay.Text)
    
        ' now calculate and overwrite.
        txPay.Text = (temp * Getdouble(txStay.Text)).ToString
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Sunday, April 1, 2018 2:10 PM
  • Something like this which does not use a case statement.

    Public Class TaxItem
        Public Property Name() As String
        Public Property Amount() As Double
        Public Overrides Function ToString() As String
            Return Name
        End Function
    End Class
    

    Form code were I hard coded the data which could easily be coming from a database table etc.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cbChoice.DataSource = new List(Of TaxItem) From 
            {
                new TaxItem() With{.Name = "VIP",.Amount = 750d},
                new TaxItem() With{.Name = "Kelas I",.Amount = 500d},
                new TaxItem() With{.Name = "Kelas II",.Amount = 300d},
                new TaxItem() With{.Name = "Kelas III",.Amount = 150d
            }}
    
    End Sub

    TextChanged event

    Private Sub txStay_TextChanged(sender As Object, e As EventArgs) Handles txStay.TextChanged
        if Not string.IsNullOrWhiteSpace(txStay.Text)
            Dim value as Integer 
            if Integer.TryParse(txStay.Text,value)
                txtPay.Text = (CType(cbChoice.SelectedItem,TaxItem).Amount * value).ToString("n2")
            else
                txtPay.Text = ""
            End If
        End If
    End Sub
    What is not covered and not asked is what happens if there is a value present and the ComboBox selection changes, might want to think about that too.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, April 1, 2018 2:12 PM
    Moderator
  • Hi

    Here is a revised version of the code, I just noticed you were asking for an 'immediate' display of result.

    ' Form1 with txChoice, txPay and txStay
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim temp As Double = 0D
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With txChoice
          .Items.Add("VIP")
          .Items.Add("Kelas I")
          .Items.Add("Kelas II")
          .Items.Add("Kelas III")
        End With
      End Sub
      Private Sub txChoice_SelectedIndexChanged(sender As Object, e As EventArgs) Handles txChoice.SelectedIndexChanged
        Dim cb As ComboBox = DirectCast(sender, ComboBox)
        Select Case cb.SelectedItem.ToString
          Case "VIP"
            txPay.Text = "750,000"
          Case "Kelas I"
            txPay.Text = "500,000"
          Case "Kelas II"
            txPay.Text = "300,000"
          Case "Kelas III"
            txPay.Text = "150,000"
        End Select
        ' store the value before overwriting it.
        temp = Getdouble(txPay.Text)
      End Sub
      Function Getdouble(s As String) As Double
        Dim v As Double = 0D
        If Double.TryParse(s, v) Then Return v
        Return 0D
      End Function
      Private Sub txStay_TextChanged_1(sender As Object, e As EventArgs) Handles txStay.TextChanged
        ' calculate and overwrite.
        txPay.Text = (temp * Getdouble(txStay.Text)).ToString
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by Mr. Monkeyboy Sunday, April 1, 2018 4:23 PM
    Sunday, April 1, 2018 2:50 PM
  • Then when I add value of textbox 'txStay' as Integer, ...

    How does this happen?  If you mean "Whenever I type anything inside the text box 'txtStay' I want 'txtPay' to be updated"? That would be a Keydown event for txtStay.   Or if you mean "Whenever I finish typing a value into 'txtStay' I want 'txtPay' to be updated. That would be a Validating event for 'txtStay'.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown(v=vs.110).aspx
    https://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating(v=vs.110).aspx

    Sunday, April 1, 2018 9:48 PM
  • I mean when i click the textbox nothing's happen
    Monday, April 2, 2018 12:46 PM
  • I mean when i click the textbox nothing's happen
    What are you expecting to happen?  You would not normally use an event for clicking in a text box.  The usual events for a text box are KeyDown or Validate.  What textbox event are you using, and what is the code?
    Monday, April 2, 2018 12:53 PM