vb.net - 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.
解决方案
执行此操作的正确方法是验证两个输入,并且仅在用户完成输入两个有效输入时才执行求和,例如
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
请注意,算术不会在用户键入时发生,而是在他们离开控件时发生。无需单击 aButton
但焦点必须离开TextBoxes
. 如果您真的愿意,您可以在输入更改时同时进行验证和算术运算,但我并没有真正看到向用户显示他们不感兴趣的结果的意义。
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
推荐阅读
- python - 如何在 python Keras 中实现自定义指标“负预测值”?
- java - 在相同颜色背景上滚动时如何更改 textView 颜色?
- java - 如何转换 ArrayList
到数组列表 ? - javascript - 在 ejs 中动态设置 data-id 的值
- xunit - AutoFixture 可以为作为该类属性的每个可能的枚举创建一个对象列表吗?
- python - sklearn.linear_model.Lasso 可以识别熊猫数据框中的分类数据类型吗?
- javascript - 如何在按钮标签上添加间隔?
- python - 可以使用什么样的对象`yield from`?
- java - 用于测试 java gitlab CI/CD 的所有单元测试的命令
- python - 使用 executemany 和 partialy 参数化列表插入