单击VB.net中的“添加”按钮后,向文本框添加值

问题描述 投票:0回答:4

这是我的部分编码...我想要的是当我点击按钮时,数据库中的值将被添加到文本框中。但是当我单击按钮时,我得到的文本框中的值直接被数据库中的新值替换。有人可以帮我解决这个问题吗?

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        txtShowDescrip.Text &= cmbDescrip.SelectedItem & ","

        Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=SDP_DB.accdb")
        Dim str As String
        Dim dr As OleDbDataReader
        Dim cmd As OleDbCommand
        Dim total As Integer = 0
        conn.Open()
        str = "Select * From ChargeTable Where Description='" & cmbDescrip.Text & "'"

        cmd = New OleDbCommand(str, conn)
        dr = cmd.ExecuteReader
        dr.Read()
        If dr.HasRows = True Then

            total += dr.Item("PriceRate")
            txtShowRate.Text = "$" & total

        End If
        dr.Close()
        conn.Close()
    End Sub
vb.net vb.net-2010
4个回答
0
投票

Omg,阅读你自己的帖子;

...当我点击按钮时,文本框中的值将被添加到文本框中。但是当我单击按钮时,文本框中的值将直接替换为新值。

那么点击该按钮到底想要发生什么?


0
投票

您当前将文本框的文本设置为新值,如果要添加需要使用的新内容和/或&=

    Dim nfi As New System.Globalization.NumberFormatInfo()
    nfi.CurrencySymbol = "$"

    dim tot as decimal = total + Decimal.Parse(TextBox1.Text, Globalization.NumberStyles.Any, nfi)


    txtShowRate.Text =  "$" & tot 

0
投票

你可以使用静态声明..

Static total As Integer = 0

或者,如果你仍然使用Dim ..

If dr.HasRows = True Then

    total = val(txtShowRate.Text) + dr.Item("PriceRate")
    txtShowRate.Text = "$" & format(total)

End If

0
投票
If dr.HasRows = True Then
        total = CType(txtShowRate.Text,Integer) + dr.Item("PriceRate")
        txtShowRate.Text = "$" & total.ToString
End If
© www.soinside.com 2019 - 2024. All rights reserved.