如何在vb.net中使用计时器

时间:2012-10-29 05:50:11

标签: 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

发生的是,消息框一遍又一遍地出现。如何触发消息框一旦完成后自动关闭?我评论了“else”中的代码,因为它反复重复。

2 个答案:

答案 0 :(得分:2)

您必须使用自定义消息框。普通的消息框不会做你想要的事情。它会每2秒弹出一次。最好的选择是制作一个新表格并将其显示为一个消息框。 :)

答案 1 :(得分:0)

您需要在timer1.tick处理程序中设置timer1.enabled = false