每次单击时更改变量值

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

我有一个变量“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)

结束子

vb.net
2个回答
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

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 
© www.soinside.com 2019 - 2024. All rights reserved.