VB程序,循环迭代和excel写入

时间:2011-10-20 03:23:23

标签: vb.net

我正在为实习编写一个程序,需要一些建议。我完成了我的研究,但大部分都没有结果......我需要循环“buttonOneClick进行一秒迭代。程序将发送一个”P“字符,等待一秒钟,发送ap,等待一秒等等。此外,我还需要将收到的信息写入excel电子表格。对现有代码的任何帮助/批评都将不胜感激。

这就是我所拥有的:

Public Class Form2

Dim buttonOnePush As Boolean = False
Dim buttonTwoPush As Boolean = False



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Send strings to a serial port.

    Using com5 As IO.Ports.SerialPort =
            My.Computer.Ports.OpenSerialPort("COM5")
        com5.WriteLine("P")

    End Using

End Sub

Function ReceiveSerialData() As String
    ' Receive strings from a serial port.
    Dim returnStr As String = ""

    Dim com5 As IO.Ports.SerialPort = Nothing
    Try
        com5 = My.Computer.Ports.OpenSerialPort("COM5")
        com5.ReadTimeout = 10000
        Do
            Dim Incoming As String = com5.ReadLine()
            If Incoming Is Nothing Then
                Exit Do
            Else
                returnStr &= Incoming & vbCrLf
            End If
        Loop
    Catch ex As TimeoutException
        returnStr = "Error: Serial Port read timed out."
    Finally
        If com5 IsNot Nothing Then com5.Close()
    End Try

    Return returnStr
End Function


Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    If IsNumeric(TextBox1.Text) AndAlso IsNumeric(TextBox2.Text) Then
        TextBox1.Text = CDec(TextBox2.Text)
    End If
End Sub

Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
    If IsNumeric(TextBox6.Text) AndAlso IsNumeric(TextBox3.Text) Then
        TextBox6.Text = CDec(TextBox3.Text)
    End If
End Sub

Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
    If IsNumeric(TextBox7.Text) AndAlso IsNumeric(TextBox4.Text) Then
        TextBox7.Text = CDec(TextBox4.Text)
    End If
End Sub

Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged
    If IsNumeric(TextBox8.Text) AndAlso IsNumeric(TextBox5.Text) Then
        TextBox8.Text = CDec(TextBox5.Text)
    End If
End Sub

Private Sub TextBox15_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox15.TextChanged
    If IsNumeric(TextBox15.Text) AndAlso IsNumeric(TextBox16.Text) Then
        TextBox15.Text = Hex(TextBox16.Text)
    End If
End Sub

Private Sub TextBox14_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox14.TextChanged
    If IsNumeric(TextBox14.Text) AndAlso IsNumeric(TextBox11.Text) Then
        TextBox14.Text = Hex(TextBox11.Text)
    End If
End Sub

Private Sub TextBox13_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox13.TextChanged
    If IsNumeric(TextBox13.Text) AndAlso IsNumeric(TextBox10.Text) Then
        TextBox13.Text = Hex(TextBox10.Text)
    End If
End Sub

Private Sub TextBox12_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox12.TextChanged
    If IsNumeric(TextBox12.Text) AndAlso IsNumeric(TextBox9.Text) Then
        TextBox12.Text = Hex(TextBox9.Text)
    End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    buttonTwoPush = True
    buttonOnePush = False

    Me.Close()
    Form1.Close()

End Sub

结束班

1 个答案:

答案 0 :(得分:0)

使用Timer以一秒为间隔发出该命令。从工具箱中将计时器拖到表单上,然后双击它以获得_Tick方法。

在表单的构造函数中设置计时器的.Interval成员,并使用.Start.Stop方法来控制它。

对于Excel文件,您需要添加对Microsoft Excel 12.0(或14.0,如果您有Excel 2010)对象库的项目的引用。通过右键单击解决方案资源管理器中的项目,可以在Add Reference对话框的COM选项卡下找到此内容。有关详尽的参考信息,请参阅this page(向下滚动到页面底部以获取VB.NET中的快速示例)。