2016-12-30 61 views
-2

我有一個關於使用for循環的問題。下面是一個使用數據庫過濾代碼Visual Basic 2008的For循環使用計數器

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 

我的問題是,當搜索沒有發現3次,顯示一些MSGBOX

所以放在哪裏trytime循環?

+0

你是什麼意思*三倍*?這個程序會被調用3次,然後你想顯示消息?如果是,那麼變量作用域被錯誤地定義。它應該在這個過程之外,你應該從這個過程中增加它的價值。 – Spidey

+0

我的意思是,如果用戶嘗試三次輸入錯誤的數字,然後** MSGBOX **似乎告訴他,他已經嘗試了3次,並且是真正 –

回答

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 
+0

它的作品thanx,但有一個小錯誤 'trytime + = 1'如果沒有找到,則增加'必須在第二個'if'之前 –

+0

對,我的錯誤:) – Spidey

+0

實際上,它應該在FOR循環之外,以避免過度增加值。 – Spidey