使用计时器将文本框行发送到记事本
Using timer to send textbox lines to notepad
我是 VB 新手,正在使用计时器编写一个小练习。
经过多次尝试,我无法让它工作。我需要将 RichTextBox (txtcontent
) 的每一行发送到打开的记事本。
我将定时器间隔设置为 1000 毫秒(1 秒),计算文本框行数,然后发送(首先我尝试使用消息框)。但是,每次 msgbox 只显示第一行并不断重复。请纠正我。 ~下面是我的定时器代码:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Static j As Integer = 0
Dim lineCount As Integer = txtcontent.Lines.Length
If j <= lineCount - 1 Then
MsgBox(txtcontent.Lines(j)) 'textbox line
End If
j += 1
End Sub
我认为您遇到的问题是在您单击消息框之前计时器一直在触发,即存在重新进入问题。
如果您在进入子例程时禁用计时器并在末尾启用它,您将看到它确实在各行中循环:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Static j As Integer = 0
Dim lineCount As Integer = txtContent.Lines.Length
If j <= lineCount - 1 Then
MsgBox(txtContent.Lines(j)) 'textbox line
End If
j += 1
Timer1.Enabled = True
End Sub
将每一行发送到记事本有点复杂。尽管理论上记事本确实支持 StandardInput,但要使其正常工作存在一些问题,因此可以改用 SendKeys:
Private _notePadProcess As Process = Nothing
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Static j As Integer = 0
Dim lineCount As Integer = txtContent.Lines.Length
If j <= lineCount - 1 Then
WriteLineToNotePad(txtContent.Lines(j))
End If
j += 1
Timer1.Enabled = True
End Sub
<DllImport("user32.dll")>
Private Shared Function SetForegroundWindow(hWnd As IntPtr) As Boolean
End Function
Private Sub WriteLineToNotePad(line As String)
If _notePadProcess Is Nothing OrElse _notePadProcess.HasExited OrElse _notePadProcess.MainWindowHandle = IntPtr.Zero Then
Dim startInfo As New ProcessStartInfo("notepad.exe")
_notePadProcess = Process.Start(startInfo)
Do Until _notePadProcess.MainWindowHandle <> IntPtr.Zero
'wait
Loop
End If
SetForegroundWindow(_notePadProcess.MainWindowHandle)
SendKeys.Send(line + vbCr)
End Sub
我是 VB 新手,正在使用计时器编写一个小练习。
经过多次尝试,我无法让它工作。我需要将 RichTextBox (txtcontent
) 的每一行发送到打开的记事本。
我将定时器间隔设置为 1000 毫秒(1 秒),计算文本框行数,然后发送(首先我尝试使用消息框)。但是,每次 msgbox 只显示第一行并不断重复。请纠正我。 ~下面是我的定时器代码:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Static j As Integer = 0
Dim lineCount As Integer = txtcontent.Lines.Length
If j <= lineCount - 1 Then
MsgBox(txtcontent.Lines(j)) 'textbox line
End If
j += 1
End Sub
我认为您遇到的问题是在您单击消息框之前计时器一直在触发,即存在重新进入问题。
如果您在进入子例程时禁用计时器并在末尾启用它,您将看到它确实在各行中循环:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Static j As Integer = 0
Dim lineCount As Integer = txtContent.Lines.Length
If j <= lineCount - 1 Then
MsgBox(txtContent.Lines(j)) 'textbox line
End If
j += 1
Timer1.Enabled = True
End Sub
将每一行发送到记事本有点复杂。尽管理论上记事本确实支持 StandardInput,但要使其正常工作存在一些问题,因此可以改用 SendKeys:
Private _notePadProcess As Process = Nothing
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Static j As Integer = 0
Dim lineCount As Integer = txtContent.Lines.Length
If j <= lineCount - 1 Then
WriteLineToNotePad(txtContent.Lines(j))
End If
j += 1
Timer1.Enabled = True
End Sub
<DllImport("user32.dll")>
Private Shared Function SetForegroundWindow(hWnd As IntPtr) As Boolean
End Function
Private Sub WriteLineToNotePad(line As String)
If _notePadProcess Is Nothing OrElse _notePadProcess.HasExited OrElse _notePadProcess.MainWindowHandle = IntPtr.Zero Then
Dim startInfo As New ProcessStartInfo("notepad.exe")
_notePadProcess = Process.Start(startInfo)
Do Until _notePadProcess.MainWindowHandle <> IntPtr.Zero
'wait
Loop
End If
SetForegroundWindow(_notePadProcess.MainWindowHandle)
SendKeys.Send(line + vbCr)
End Sub