添加2个文本框的值以在其中一个显示结果 [英] Adding the values of 2 textboxes to display result in one of them
问题描述
在我的表单上,我有2个文本框,分别为 txtsurcharges.text和 txttotal.text。
On my form I have 2 textboxes called 'txtsurcharges.text' and 'txttotal.text'
txttotal从mysql表中检索一个值,但是我想要发生的是,当我在txtsurcharges中输入一个值并在txttotal中显示结果时,该值将被添加。
txttotal retrieves a value from mysql table, but what I want to happen is that this value becomes added to when I type in a value into txtsurcharges and display the result in txttotal.
这是我的txtsurcharges代码:
Here is my code for txtsurcharges:
txtsurcharges.Text = String.Format("£{0}", txtsurcharges.Text)
这是我的txttotal代码:
Here is my code for txttotal:
Private Sub cbxPaymentID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxPaymentID.SelectedIndexChanged
Dim dss As New DataSet
If (cbxPaymentID.SelectedIndex <> -1) Then
Dim daa As New MySqlDataAdapter("SELECT * from payment_details WHERE PaymentID=@PID", _
objconnection)
daa.SelectCommand.Parameters.AddWithValue("@PID", Convert.ToInt32(cbxPaymentID.SelectedValue))
daa.Fill(dss)
txttotal.Text = dss.Tables(0).Rows(0)("ServicePayment").ToString()
End If
End Sub
这是我添加两个值的代码,不起作用:
Here is my code for the addition of the 2 values, which doesn't work:
Private Sub txtsurcharges_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsurcharges.TextChanged
Dim c As Integer
c = Val(txtsurcharges.Text) + Val(txttotal.Text)
c = txttotal.Text
End Sub
推荐答案
尝试一下(更新以显示ASPX和VB代码,并带有几个静态值进行测试)
Try this (UPDATED TO SHOW ASPX and VB code with a couple of static values for test)
aspx代码
<asp:TextBox ID="txtsurcharges" runat="server" Text="£5" AutoPostBack="true"></asp:TextBox>
<asp:TextBox ID="txttotal" runat="server" Text="£5"></asp:TextBox>
vb代码
Private Sub txtsurcharges_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsurcharges.TextChanged
Dim c As Integer
c = CInt(txtsurcharges.Text) + CInt(txttotal.Text)
txttotal.Text = c
End Sub
您可以
如果我现在将txtsurcharges更改为10,则txttotal将更改为15。
If I now change txtsurcharges to 10 then txttotal will change to 15.
当然,您需要确保只能在txtsurcharges中键入数字。
You will of course need to make sure that only numerics can be typed into txtsurcharges.
这篇关于添加2个文本框的值以在其中一个显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!