使用For循环的计数器

时间:2016-12-30 09:49:52

标签: vb.net

我有一个关于使用for循环的问题。下面是使用visual basic 2008进行数据库过滤的代码。

Private Sub txtsearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtsearch.KeyDown
    On Error Resume Next
    If e.KeyCode = Keys.Enter Then
        ''  Me.Table1BindingSource.Filter = "EmpID = ' " & Me.txtsearch.Text & "'"
        On Error Resume Next
        Dim temp As Integer = 0
        Dim trytime As Integer = 0

        Me.Table1BindingSource.Filter = "EmpID = ' " & Me.txtsearch.Text & "'"

        For i As Integer = 0 To Table1DataGridView.RowCount - 1
            For j As Integer = 0 To Table1DataGridView.ColumnCount - 1

                If Table1DataGridView.Rows(i).Cells(j).Value.ToString = txtsearch.Text Then
                    ''if item found then we play sound ok
                    My.Computer.Audio.Play("F:\beep.wav", AudioPlayMode.WaitToComplete)
                    My.Computer.Audio.Play("F:\beep.wav", AudioPlayMode.WaitToComplete)

                    temp = 1
                End If
            Next
        Next
        If temp = 0 Then

            ''if item not found then we play sound err
            My.Computer.Audio.Play("F:\computer_access.wav", AudioPlayMode.WaitToComplete)
            Me.Table1TableAdapter.Fill(Me.MydbDataSet.Table1)
            Me.Table1DataGridView.Refresh()
            txtsearch.Text = ""

        End If

    End If
End Sub

我的问题是找不到三次搜索,所以显示一些 MSGBOX

那么在哪里放置 trytime 循环?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:查看评论

Public Class Form1

    'Define the variable within the class scope
    Dim trytime As Integer = 0

    Private Sub txtsearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtsearch.KeyDown
        On Error Resume Next
        If e.KeyCode = Keys.Enter Then
            ''  Me.Table1BindingSource.Filter = "EmpID = ' " & Me.txtsearch.Text & "'"
            On Error Resume Next
            Dim temp As Integer = 0

            Me.Table1BindingSource.Filter = "EmpID = ' " & Me.txtsearch.Text & "'"

            For i As Integer = 0 To Table1DataGridView.RowCount - 1
                For j As Integer = 0 To Table1DataGridView.ColumnCount - 1

                    If Table1DataGridView.Rows(i).Cells(j).Value.ToString = txtsearch.Text Then
                        ''if item found then we play sound ok
                        My.Computer.Audio.Play("F:\beep.wav", AudioPlayMode.WaitToComplete)
                        My.Computer.Audio.Play("F:\beep.wav", AudioPlayMode.WaitToComplete)

                        temp = 1


                    End If
                Next
            Next

            If temp = 0 Then 
                trytime += 1 'Increment if not found

                If trytime >= 3 then 'Check if not found 3 times (or more)
                  'Take action when not found 3 times
                End If

                ''if item not found then we play sound err
                My.Computer.Audio.Play("F:\computer_access.wav", AudioPlayMode.WaitToComplete)
                Me.Table1TableAdapter.Fill(Me.MydbDataSet.Table1)
                Me.Table1DataGridView.Refresh()
                txtsearch.Text = ""

            End If
        End If
    End Sub
End Class