vb.net 将值增加一个视觉基础卡住了
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15924841/
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
incrementing a value by one visual basics am stuck
提问by benard mwangi
I am trying to write a code that will increment a value by one after clicking a button in visual basic.these are the codes i have tried so far but i have not achieved my objective
我正在尝试编写一个代码,在单击 Visual Basic 中的按钮后将值加一。这些是我迄今为止尝试过的代码,但我没有实现我的目标
code 1
代码 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim votecount As Integer = Integer.Parse(showcountwinnie.Text)
votecount += 1
showcountwinnie.Text = votecount.ToString()
End Sub
code two
代码二
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim votes As Integer
votes = 0
votes += 1
votes = showcountwinnie.Text
End Sub
code three
代码三
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim votewinnie As Integer
votewinnie = showcountwinnie.value
votewinnie = votewinnie + 1
showcountwinnie.value = votewinnie
End Sub
回答by George
Quick and dirty way:
快速而肮脏的方式:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
showcountwinnie.Text = (Val(showcountwinnie.Text) + 1).ToString()
End Sub
A more elegant way:
更优雅的方式:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim votecount As Integer
If Integer.TryParse(txt_UTC.Text, votecount) Then
votecount += 1
Else
' whatever you want to do with votecount
votecount = 0
End If
txt_UTC.Text = votecount.ToString()
End Sub
回答by Jontatas
Not the most elegant solution but working:
不是最优雅的解决方案,但工作:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim vote As Integer
If showcountwinnie.Text <> "" Then
vote = Val(showcountwinnie.Text)
Else
vote = 0
End If
vote += 1
showcountwinnie.Text = vote
End Sub
回答by Abdul Saboor
Using above suggested code, I'm posting the code which really worked for me with minor change
使用上面建议的代码,我发布了对我有用的代码,但稍作改动
Private Sub txtQuantity_Click()
Dim vote As Integer
If txtQuantity.Text <> "" Then
vote = Val(txtQuantity.Text)
Else
vote = 0
End If
vote = vote + 1
txtQuantity.Text = vote
End Sub
回答by Geo Paul
The Code 1 seems to be correct. Just try keeping a breakpoint at line 1 and check whether the code lines are getting executed while you click on the button
代码 1 似乎是正确的。只需尝试在第 1 行保留一个断点,然后在单击按钮时检查代码行是否正在执行
回答by dbasnett
Try this
尝试这个
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Static votecount As Integer = 0
votecount += 1
showcountwinnie.Text = votecount.ToString()
End Sub
回答by Kasnady
showcountwinnie.Text = showcountwinnie.text + 1
This method is using itself to +1 every click
此方法使用自身来为每次点击 +1
回答by Manny265
Kinda late but ur code 1 wont work the Parse method needs two arguments.
In code two you are meant to assign the things the other way around from the look of things like showcountwinnie.Text = votes
What is this showcountwinnie anyway? a textbox? a label?
有点晚了,但你的代码 1 不起作用 Parse 方法需要两个参数。
在代码 2 中,您打算从诸如showcountwinnie.Text = 投票之类的事物的外观中以相反的方式分配事物
无论如何,这个 showcountwinnie 是什么?一个文本框?一个标签?