2012-10-29 35 views
-1

我有這樣的代碼:如何使用定時器在vb.net

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
     Dim num As String 
     Dim message As String 
     Dim name As String 
     message = txtMessage.Text 
     Dim count As Integer = Me.TblContactsBindingSource.Count 
     If i < TblContactsDataGridView.Rows.Count - 1 Then 'stay within bounds 

     i = i + 1 ' for all rows except Row0 

     TblContactsDataGridView.Rows(i - 1).DefaultCellStyle.BackColor = Color.White ' restore previous highlight 
     TblContactsDataGridView.Rows(i).DefaultCellStyle.BackColor = Color.Bisque 'new highlight 
     num = Me.TblContactsDataGridView.Rows(i).Cells(1).Value.ToString() 
     name = Me.TblContactsDataGridView.Rows(i).Cells(0).Value.ToString() 
     If SerialPort1.IsOpen() Then 
      SerialPort1.Write("AT" & vbCrLf) 
      SerialPort1.Write("AT+CMGF=1" & vbCrLf) 

      SerialPort1.Write("AT+CMGS=" & Chr(34) & num & Chr(34) & vbCrLf) 
      SerialPort1.Write(message & Chr(26)) 
      MessageBox.Show("Message has been successfully sent to " & vbNewLine & name & " (" & num & ") ", "Message Sent", MessageBoxButtons.OK, MessageBoxIcon.Information) 


     End If 

    Else   'next row is off the bottom so 
     'i = 0  'reset index 
     'TblSmsDataGridView.Rows(TblSmsDataGridView.Rows.Count - 1).DefaultCellStyle.BackColor = Color.White 'restore bottom row 
     'TblSmsDataGridView.Rows(i).DefaultCellStyle.BackColor = Color.Bisque 'highlight top row 

    End If 

在命令按鈕,我有這樣的:

Timer1.Interval = 2000 
Timer1.Enabled = True 'no need to enable it and start it; one or t'other 

發生什麼事是,出現消息框一遍又一遍。如何觸發消息框一旦完成就自動關閉?我評論了「其他」中的代碼,因爲它反覆重複。

回答

2

您必須使用自定義消息框。正常的消息框不會做你想要的東西。它會每2秒彈出一次。最好的選擇是製作一個新表格並將其顯示爲一個消息框。 :)

+0

我可以問一個忙怎樣才能創建自定義消息框。只是一點點,以便我能理解。 – Joannou

+0

@Joannou看看這個[CodeProject文章](http://www.codeproject.com/Articles/17253/A-Custom-Message-Box),它有一個自定義消息框的實現與計時器關閉,如果有沒有反應。聽起來像你想要的 –

0

您需要在timer1.tick處理程序中設置timer1.enabled = false