2014-02-17 86 views
0

在我的形式之一,我有2個文本框稱爲「txtsurcharges.text」和「txttotal.text」加入2個文本框的值顯示結果他們

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 [email protected]", _ 
      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 

回答

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

你當然需要確保只有NUMERICS可以輸入到txtsurcharges。

+0

我收到錯誤「從字符串轉換」£「類型'整數'是無效的」? – Livaren

+1

啊...你應該試試你的Val()sytax然後...我雖然在你的文本框中有數字。我會親自將英文字符粘貼到文本框外,並將文本框限制爲數字。 – Mych

+0

這是什麼意思?我想在文本框之外放置英鎊更容易,但是我已經爲整個文本框完成了它,因爲它用英鎊獲取了值。 – Livaren

相關問題