开发者

Iteration of a text file in Visual Basic

开发者 https://www.devze.com 2023-03-23 07:01 出处:网络
Ok, so I\'m making a program that will read from a text file, convert each line into an array, the. Send each of those lines 1 pe开发者_开发知识库r tick. This is in 2010 Visual Basic.

Ok, so I'm making a program that will read from a text file, convert each line into an array, the. Send each of those lines 1 pe开发者_开发知识库r tick. This is in 2010 Visual Basic. Closest I've gotten is sending all at once, I worked on it over night and am slowly destroying it. Ideally, I want Button 1 click to populate the array from the file at LocationTB then start the timer. The timer should send a line at a time on the GapTB interval.

Public Class Form1
Public TextLine As String
Public MyFileName As String
Public MyNewLine(1000) As String
Private Property z As Integer
Private Property objReader As Object


Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If RadioButton2.Checked = True Then
        Dim Textline As String = ""
        Dim FILE_NAME As String = LocationTB.Text
        Dim objReader As New System.IO.StreamReader(FILE_NAME)
        MyFileName = LocationTB.Text
        FileOpen(1, MyFileName, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
        z = 0

        Do Until EOF(1)
            MyNewLine(z) = LineInput(1)
            z = z + 1 
        Loop

        FileClose(1)
    End If

    Timer1.Interval = GapTB.Text
    Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If RadioButton1.Checked = True Then
        AppActivate(Hook.Text)
        SendKeys.Send(SimpleTB.Text)
        SendKeys.Send((Chr(13)))

    ElseIf RadioButton2.Checked = True Then
        For Each 
            TextLine = TextLine & objReader.ReadLine
            AppActivate(Hook.Text)
            SendKeys.Send(TextLine)
            SendKeys.Send((Chr(13)))
        Next

    Else

            MsgBox("File Does Not Exist")

    End If


End Sub


Is this the kind of thing you're looking for? It'll write the contents of a file (in this instance "C:\mark.txt") to the output window in Visual Studio.

Public Class Form1
    Private myTimer As Timer

    Private lines As String()
    Private currentLine As Integer

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        lines = IO.File.ReadAllLines("C:\mark.txt")
        currentLine = 0

        myTimer = New Timer()

        AddHandler myTimer.Tick, AddressOf myTimer_Tick

        myTimer.Interval = 1000
        myTimer.Enabled = True
    End Sub

    Private Sub myTimer_Tick(sender As Object, e As EventArgs)
        If currentLine < lines.Count Then
            Dim lineToSend As String = lines(currentLine)
            Debug.Print(lineToSend)
            currentLine += 1
        Else
            myTimer.Enabled = False
        End If
    End Sub
End Class

Caveat The above code isn't scalable. If you're SURE the file will always be small then it will do (that said, they're never always small).

To make this scalable you'd need to hold the file open, and read each line as you need it, not load the entire file contents at once.


I know how to do it to a application I started myself: Here's the code to make Calculator work:

   Dim ProcID As Integer
    ' Start the Calculator application, and store the process id.
    ProcID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
    ' Activate the Calculator application.
    AppActivate(ProcID)
    ' Send the keystrokes to the Calculator application.
    My.Computer.Keyboard.SendKeys("22", True)
    My.Computer.Keyboard.SendKeys("*", True)
    My.Computer.Keyboard.SendKeys("44", True)
    My.Computer.Keyboard.SendKeys("=", True)
    ' The result is 22 * 44 = 968.

AppActivate should know the exact title of the form to activate. If not known, iterate the Processes to locate yours. If the title is correctly specified it should work

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号