Vb.net 实时添加多个文本框

Vb.net 实时添加多个文本框,vb.net,math,textbox,real-time,Vb.net,Math,Textbox,Real Time,当我运行代码时,我能够成功地将值输入到其他文本框中,但它不能实时将所有值相加到文本框(txttotalcount)中。它保持为一个空白文本框 我已尝试使用txttotalcount\u文本更改。我读过的所有其他资料都使用了单击按钮,但我希望算法能够实时执行(无需单击按钮) 我将文本框定义为在按下按钮时增加+1增量: Private Sub btnPMN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handle

当我运行代码时,我能够成功地将值输入到其他文本框中,但它不能实时将所有值相加到文本框(txttotalcount)中。它保持为一个空白文本框

我已尝试使用txttotalcount\u文本更改。我读过的所有其他资料都使用了单击按钮,但我希望算法能够实时执行(无需单击按钮)

我将文本框定义为在按下按钮时增加+1增量:

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
然后我尝试获取这些文本框值并将其添加到最终的文本框(txttotalcount):

我想将所有文本框合并成一个名为txttotalcount.text的最终文本框,实时(无按钮点击)


当我运行代码时,txTotalCount保持空白,尽管其他文本框中有值。

正确的方法是验证两个输入,并仅在用户完成输入两个有效输入时执行求和,例如

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
请注意,算法不会在用户键入时发生,而是在用户离开控件时发生。无需单击
按钮
,但焦点必须离开
文本框
。如果您真的愿意,您可以在输入发生变化时进行验证和算术,但我并不认为显示用户不感兴趣的结果有什么意义

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

正确的方法是验证两个输入,并仅在用户完成输入两个有效输入时执行求和,例如

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
请注意,算法不会在用户键入时发生,而是在用户离开控件时发生。无需单击
按钮
,但焦点必须离开
文本框
。如果您真的愿意,您可以在输入发生变化时进行验证和算术,但我并不认为显示用户不感兴趣的结果有什么意义

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

请不要使用Val。这是vb6的遗留问题。在处理用户输入时,TryParse是一个不错的选择,因为您不能依赖用户输入您想要的内容

看看你用来求和的事件。是什么更改了文本,从而触发此事件?如果您可以让它触发,您可能正在创建循环引用,因为您正在Texttotalcount.TextChanged事件中更改TXTotalCount的文本

在按钮单击中更新您的总数

正在调整这行代码

txttotalcount.text = txtPMN.Text + txtBand.Text
这不会让你得到你期望的结果。.Text属性是一个字符串。当编译器看到加号时,它将假定您想要连接字符串。假设一个文本框中有一个5,另一个文本框中有一个7。txttotalcount将显示57。"5" + "7". 如果你想做加法,你必须使用数字而不是字符串

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

请不要使用Val。这是vb6的遗留问题。在处理用户输入时,TryParse是一个不错的选择,因为您不能依赖用户输入您想要的内容

看看你用来求和的事件。是什么更改了文本,从而触发此事件?如果您可以让它触发,您可能正在创建循环引用,因为您正在Texttotalcount.TextChanged事件中更改TXTotalCount的文本

在按钮单击中更新您的总数

正在调整这行代码

txttotalcount.text = txtPMN.Text + txtBand.Text
这不会让你得到你期望的结果。.Text属性是一个字符串。当编译器看到加号时,它将假定您想要连接字符串。假设一个文本框中有一个5,另一个文本框中有一个7。txttotalcount将显示57。"5" + "7". 如果你想做加法,你必须使用数字而不是字符串

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
这是另一种方法:

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
这是另一种方法:

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

或者,您也可以将其用作替代方案:

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

或者,您也可以将其用作替代方案:

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

你的代码不好!如果要添加数字,请添加数字,不要连接字符串。控件的
Text
属性是一个
字符串
,添加两个
字符串
将它们连接起来,例如
“1”+“1”
是“11”,而不是2。我知道。这实际上是我第一次尝试用vb编程。到目前为止,我所做的一切都是通过反复试验,查找其他代码,并尽可能地学习和拼凑。我可能应该买本书或者去上课。有很多教程至少可以让你了解基本知识,例如。从那以后,你会对你不知道的东西有一个更好的了解,需要专门研究。你的代码很糟糕!如果要添加数字,请添加数字,不要连接字符串。控件的
Text
属性是一个
字符串
,添加两个
字符串
将它们连接起来,例如
“1”+“1”
是“11”,而不是2。我知道。这实际上是我第一次尝试用vb编程。到目前为止,我所做的一切都是通过反复试验,查找其他代码,并尽可能地学习和拼凑。我可能应该买本书或者去上课。有很多教程至少可以让你了解基本知识,例如。从那以后,你会对你不知道的东西有一个更好的了解,并且需要专门研究。这一个非常接近于给我我想要的。但是,当我单击按钮向Band添加+1增量时,它将保持为“1”。PMN继续开火,txttotalcount正在捕获它。这一个离我想要的东西太近了。但是,当我单击按钮向Band添加+1增量时,它将保持为“1”。PMN继续开火,TXTotalCount正在捕获它。太棒了!这一个做到了!谢谢你的帮助!我从中学到了一些东西。非常感谢,太好了!这一个做到了!谢谢你的帮助!我从中学到了一些东西。非常感谢。