每次点击更改变量值

时间:2014-07-24 08:10:39

标签: vb.net

我有一个变量" row"我想在每个btnClick上改变它的值如下:{1,1,1,1,2,3,4,4,4,5,4..etc}

根据我的意愿,我的解决方案不适用于每次鼠标点击:

Dim row As Integer

Private Sub incrementVariable1(ByVal x As Integer)
    row= 1
End Sub
Private Sub incrementVariable2(ByVal x As Integer)
    row= 1
End Sub
Private Sub incrementVariable3(ByVal x As Integer)
    row= 2
End Sub

Private Sub btnUpdate_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdScore.Click

  incrementVariable1(row)
  incrementVariable2(row)
  incrementVariable3(row)

End Sub

2 个答案:

答案 0 :(得分:1)

我觉得非常担心没有人能够理解这个问题......!试试这个;

Dim RowData() As Integer = {1, 1, 1, 1, 2, 3, 4, 4, 4, 5, 4}
Dim Index As Integer
Dim row As Integer

Private Sub IncrementIndex(ByVal x As Integer)
    row = RowData(Index)
    Index += 1
    If Index = RowData.Length Then Index = 0
End Sub

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdScore.Click
    IncrementIndex(row)
End Sub

答案 1 :(得分:0)

我对计数{1,1,2,3,4..etc}完全感到困惑,我认为这是一个输入错误,但作为程序员,我必须这样做,看看我的代码:它给出了预期的结果

 Dim rows As Integer = 0
 Dim i As Integer = 2
 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
   rows += 1 ' Inrement rows by 1 in each click
   If i AndAlso rows = 2 Then ' true only if both i and rows are having 2
   ListBox1.Items.Add(rows - 1) ' display the result in the listbox in each click
   i = 0 ' 0 is assigned to i for make the condetion faulse for the next time
   rows -= 1 ' in this case rows is decreased by 1
   Else
   ListBox1.Items.Add(rows) ' display the result in the listbox in each click
   End If    
End Sub