Running cmd in a single textbox: how to put the cursor in the right place

42 Views Asked by At

I would like to run cmd.exe in one single textbox. When I use the code down below, everything works fine. The only thing is that the cursor is located below the prompt line, so:

C:\Users\myuser\source\repos\WindowsApp7\WindowsApp7\bin\Debug>

The cursor is here

and I would like my cursor to be next to the prompt, like:

C:\Users\mysuser\source\repos\WindowsApp7\WindowsApp7\bin\Debug>The cursor should be here

Any ideas how I should do that? Thanks for any help in advance!

Kind regards,

Eric

Public Class Form1
    Dim P As New Process
    Dim SW As System.IO.StreamWriter
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler P.OutputDataReceived, AddressOf DisplayOutput
        P.StartInfo.CreateNoWindow() = True
        P.StartInfo.UseShellExecute = False
        P.StartInfo.RedirectStandardInput = True
        P.StartInfo.RedirectStandardOutput = True
        P.StartInfo.FileName = "cmd.exe"
        P.Start()
        P.SynchronizingObject = TextBox1
        P.BeginOutputReadLine()
        SW = P.StandardInput
        SW.WriteLine()
    End Sub
    Private Sub DisplayOutput(ByVal sendingProcess As Object, ByVal output As DataReceivedEventArgs)
        TextBox1.AppendText(output.Data() & vbCrLf)
    End Sub
    Private Sub Textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Static Line As String
        If e.KeyChar = Chr(Keys.Return) Then
            SW.WriteLine(Line & vbCrLf)
            Line = ""
        Else
            Line = Line & e.KeyChar
        End If
    End Sub
End Class
0

There are 0 best solutions below