单击 VB.net 中的“添加”按钮后向文本框添加值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18266787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add value to textbox after clicking the "Add" button in VB.net
提问by YW CHAI
Here is my Part of the coding...what i want is when i clicked the button, the value from the database will be added into the textbox. But what i get when i click the button, the value in the textbox is straight replaced by the new value from the database. anybody can help me on solving this problem?
这是我的编码部分……我想要的是当我单击按钮时,数据库中的值将添加到文本框中。但是当我单击按钮时,文本框中的值直接被数据库中的新值替换。有人可以帮我解决这个问题吗?
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
回答by Lectere
Omg, read your own post back;
天啊,读回你自己的帖子;
... when i clicked the button, the value in the textbox will be added into the textbox. But when i click the button, the value in the textbox is straight replaced by the new value.
...当我单击按钮时,文本框中的值将添加到文本框中。但是当我单击按钮时,文本框中的值直接被新值替换。
So what exactly do you want to happen the the button is clicked?
那么你到底想在按钮被点击时发生什么呢?
回答by David Sdot
Your currently set the text of the textbox to the new value, if you want to add new stuff you need to use & or &=
您当前将文本框的文本设置为新值,如果要添加新内容,则需要使用 & 或 &=
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
回答by Nima Yr.
If dr.HasRows = True Then
total = CType(txtShowRate.Text,Integer) + dr.Item("PriceRate")
txtShowRate.Text = "$" & total.ToString
End If
回答by matzone
You may use static declaration ..
您可以使用静态声明..
Static total As Integer = 0
Or if you still use Dim..
或者,如果您仍然使用Dim..
If dr.HasRows = True Then
total = val(txtShowRate.Text) + dr.Item("PriceRate")
txtShowRate.Text = "$" & format(total)
End If

