2016-12-30 9 views
-2

forループの使用に関する質問があります。検索はので、いくつかのがMSGBOXForループでカウンタを使用する

はそうどこtrytimeループを置くことを表示する3回見つからない場合、以下のデータベース・フィルタリングのためのコードは、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 

私の問題をされて使用されますか?

+0

を?この手順は3回呼び出され、メッセージを表示しますか?変数スコープが間違って定義されている場合この手順の外にある必要があります。この手順から値を増やす必要があります。 – Spidey

+0

ユーザーがMSGBOX **その後、間違った番号を入力するための3つの時間をしようとした場合、私は意味**は、彼が持っていた3回を試してみてください,,とcarful –

答えて

0

このような何か試してください:あなたは* * 3回とはどういう意味ですかコメントをご確認

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

その作品をありがとうあってもよいが、一つの小さな誤差 'trytime + = 1 'で彼を伝えるために表示されます2番目の 'if 'の前にある必要があります –

+0

右、私の間違い: – Spidey

+0

実際には、値を増分することを避けるためには、FORループの外にある必要があります。 – Spidey

関連する問題