2

我正在尝试编写一个代码,在单击 Visual Basic 中的按钮后将值增加一。这些是我迄今为止尝试过的代码,但我没有实现我的目标

代码 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

代码二

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

代码三

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
4

7 回答 7

8

快速而肮脏的方式:

        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

更优雅的方式:

        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
于 2013-04-10T14:16:02.810 回答
3

不是最优雅的解决方案,但有效:

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
于 2013-04-10T11:54:52.883 回答
1

使用上面建议的代码,我发布了对我真正有用的代码,只需稍作改动

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
于 2016-08-25T17:00:46.130 回答
0

代码 1 似乎是正确的。只需尝试在第 1 行保留一个断点,并在单击按钮时检查代码行是否正在执行

于 2013-04-10T11:56:17.050 回答
0

尝试这个

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
于 2013-04-10T12:58:29.493 回答
0
showcountwinnie.Text = showcountwinnie.text + 1

此方法使用自身为每次点击 +1

于 2013-04-10T13:56:15.470 回答
-1

有点晚了,但你的代码 1 不起作用 Parse 方法需要两个参数。 在代码 2 中,您的意思是从showcountwinnie.Text = votes
之类的东西的外观相反的方式分配东西这个 showcountwinnie 到底是什么?一个文本框?一个标签?

于 2013-06-23T00:16:55.973 回答