添加2个文本框的值以显示其中一个

时间:2014-02-17 14:38:24

标签: mysql vb.net textbox add calculator

在我的表格上,我有两个名为'txtsurcharges.text'和'txttotal.text'的文本框

txttotal从mysql表中检索一个值,但我想要发生的是当我在txtsurcharges中输入一个值并在txttotal中显示结果时,该值会被添加。

这是我的txtsurcharges代码:

txtsurcharges.Text = String.Format("£{0}", txtsurcharges.Text)

这是我的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

以下是我添加2个值的代码,但不起作用:

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

1 个答案:

答案 0 :(得分:0)

试试这个(更新显示ASPX和VB代码,带有几个静态值进行测试)

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

正如您所看到的那样,您将c设置为=附加费和总额的总和,然后您将c重置为总​​数。

如果我现在将txtsurcharges更改为10,则txttotal将更改为15.

你当然需要确保只有数字可以输入txtsurcharges。