-1

我有这个代码:

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”中注释了代码,因为它一遍又一遍地重复。

4

2 回答 2

2

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

于 2012-10-29T05:54:40.330 回答
0

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

于 2012-10-29T05:57:47.110 回答