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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13116506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 10:56:17  来源:igfitidea点击:

How to use timer in vb.net

vb.net

提问by Joa.know

I have this code:

我有这个代码:

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

In a command button I have this:

在命令按钮中,我有这个:

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

What happen is, the message box appears over and over. How can i trigger message box to automatically close once it is finished? I commented the code in the "else" because the it repeats over and over.

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

回答by alan_dw

You have to use a custom message box. Normal message box wont do the thing you wanted. It will pop up every 2 second. best choice is to make a new form and show it as a message box. :)

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

回答by xpda

You need to set timer1.enabled = falsein the timer1.tick handler.

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