Newbie
Newbie

Reputation: 31

Adding multiple textboxes real time

When I run my code, I am able to successfully input values into other textboxes, but it does not sum all of the values into a textbox (txttotalcount) in real-time. It stays as a blank textbox.

I've tried using the txttotalcount_TextChanged. All other sources I've read uses the Button_Click, but I would like for the arithmetic to happen in real-time (no need to click a button).

I defined my textboxes to add +1 increment on button press:

Private Sub btnPMN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPMN.Click
    'Add +1 to PMN Textbox (txtPMN)

    txtPMN.Text = (Val(txtPMN.Text) + 1).ToString()
End Sub

Private Sub BtnBand_Click(sender As Object, e As EventArgs) Handles btnBand.Click
    'Add +1 to Band Textbox (txtBand)

    txtBand.Text = (Val(txtBand.Text) + 1).ToString()
End Sub

Then I tried taking those textbox values and adding it into a final textbox (txttotalcount):

Private Sub Txttotalcount_TextChanged(sender As Object, e As EventArgs) Handles txttotalcount.TextChanged
    'Adds all text boxes 
    txttotalcount.text = txtPMN.Text + txtBand.Text


End Sub

I would like to sum all textboxes into a final textbox called txttotalcount.text in real-time (no button clicks)

When I run my code, the txttotalcount stays blank although there are values in the other textboxes.

Upvotes: 0

Views: 1326

Answers (4)

user11982798
user11982798

Reputation: 1908

or you can use these as alternative:

Private Sub btnPMN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPMN.Click
    txtIncrement(txtPMN, 1)
    Calculate()
End Sub

Private Sub BtnBand_Click(sender As Object, e As EventArgs) Handles BtnBand.Click
    txtIncrement(txtBand, 1)
    Calculate()
End Sub

Private Sub txtIncrement(ByRef myTextBox As TextBox, increment As Integer)
    If IsNumeric(myTextBox.Text) Then myTextBox.Text = (CLng(myTextBox.Text) + increment).ToString Else myTextBox.Text = 0
End Sub

Private Sub Calculate()
    txttotalcount.Text = "0"
    Dim myBand As Long = 0
    Dim myPMN As Long = 0
    If IsNumeric(txtBand.Text) Then myBand = CLng(txtBand.Text)
    If IsNumeric(txtPMN.Text) Then myPMN = CLng(txtPMN.Text)
    txttotalcount.Text = (myBand + myPMN).ToString("#,##0")
End Sub

Upvotes: 1

user11982798
user11982798

Reputation: 1908

This another method:

Private Sub btnPMN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPMN.Click
    'Add +1 to PMN Textbox (txtPMN)
    If IsNumeric(txtPMN.Text) Then
        txtPMN.Text = (CLng(txtPMN.Text) + 1).ToString
    Else
        txtPMN.Text = "1"
    End If
    Calculate()
End Sub

Private Sub BtnBand_Click(sender As Object, e As EventArgs) Handles BtnBand.Click
    'Add +1 to Band Textbox (txtBand)
    If IsNumeric(txtBand.Text) Then
        txtBand.Text = (CLng(txtBand.Text) + 1).ToString
    Else
        txtBand.Text = "1"
    End If
    Calculate()
End Sub

Private Sub Calculate()
    txttotalcount.Text = "0"
    Dim myBand As Long = 0
    Dim myPMN As Long = 0
    If IsNumeric(txtBand.Text) Then myBand = CLng(txtBand.Text)
    If IsNumeric(txtPMN.Text) Then myPMN = CLng(txtPMN.Text)
    txttotalcount.Text = (myBand + myPMN).ToString("#,##0")
End Sub

Upvotes: 0

Mary
Mary

Reputation: 15091

Please don't use Val. That is a leftover from vb6. When dealing with user input .TryParse is a good choice because you can't depend on a user to enter what you expect.

Look at the event you are using to sum. What changes the text so this event will fire? If you could get it to fire you might be creating a circular reference because you are changing the text of Txttotalcount in the Texttotalcount.TextChanged event.

Update your total in the button clicks.

Reguarding this line of code...

txttotalcount.text = txtPMN.Text + txtBand.Text

This will not get you the result you expect. The .Text property is a String. When the compiler sees the plus sign it will assume you want to concatenate strings. Suppose you have a 5 in one textbox and a 7 in the other. txttotalcount will display 57. "5" + "7". If you wish to do addition you must use numbers not strings.

Private Sub btnPMN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPMN.Click
    Dim Band As Integer
    Dim PMN As Integer
    If Integer.TryParse(txtPMN.Text, Band) Then
        PMN += 1
        txtPMN.Text = PMN.ToString
    Else
        MessageBox.Show("Please enter a valid number in PMN")
        Exit Sub
    End If
    'Band will be zero if the parse fails
    Integer.TryParse(txtBand.Text, Band)
    txttotalcount.Text = (PMN + Band).ToString
End Sub

Private Sub BtnBand_Click(sender As Object, e As EventArgs) Handles btnBand.Click
    Dim Band As Integer
    Dim PMN As Integer
    If Integer.TryParse(txtBand.Text, Band) Then
        Band += 1
        txtBand.Text = Band.ToString
    Else
        MessageBox.Show("Please enter a valid number in Band")
        Exit Sub
    End If
    'PMN will be zero if the parse fails
    Integer.TryParse(txtPMN.Text, PMN)
    txttotalcount.Text = (PMN + Band).ToString
End Sub

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54457

The proper way to do this would be to validate the two inputs and only perform the sum when the user has completed entering both valid inputs, e.g.

Private Sub TextBoxes_Validating(sender As Object, e As ComponentModel.CancelEventArgs) Handles TextBox2.Validating,
                                                                                                TextBox1.Validating
    Dim source = DirectCast(sender, TextBox)

    If source.TextLength > 0 AndAlso Not Integer.TryParse(source.Text, Nothing) Then
        source.SelectAll()
        source.HideSelection = False

        MessageBox.Show("Please enter a valid integer")

        source.HideSelection = True

        'Don't let the control lose focus with invalid contents.
        e.Cancel = True
    End If
End Sub

Private Sub TextBoxes_Validated(sender As Object, e As EventArgs) Handles TextBox2.Validated,
                                                                          TextBox1.Validated
    If TextBox1.TextLength > 0 AndAlso TextBox2.TextLength > 0 Then
        Label1.Text = (CInt(TextBox1.Text) + CInt(TextBox2.Text)).ToString()
    End If
End Sub

Note that the arithmetic will not happen as the user types but rather when they leave a control. There's no need to click a Button but focus must leave the TextBoxes. You could do both the validation and arithmetic as the input changes if you really wanted to, but I don't really see the point in showing the user results that they have no interest in.

Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged,
                                                                            TextBox1.TextChanged
    Dim input1 As Integer
    Dim input2 As Integer

    If TextBox1.TextLength > 0 AndAlso
       TextBox2.TextLength > 0 AndAlso
       Integer.TryParse(TextBox1.Text, input1) AndAlso
       Integer.TryParse(TextBox2.Text, input2) Then
        Label1.Text = (input1 + input2).ToString()
    Else
        Label1.ResetText()
    End If
End Sub

Upvotes: 1

Related Questions