How can I update the current line in a C# Windows Console App?

前端 未结 17 1326
南旧
南旧 2020-11-22 14:43

When building a Windows Console App in C#, is it possible to write to the console without having to extend a current line or go to a new line? For example, if I want to sho

17条回答
  •  时光说笑
    2020-11-22 15:04

    i was looking for same solution in vb.net and i found this one and it's great.

    however as @JohnOdom suggested a better way to handle the blanks space if previous one is larger than current one..

    i make a function in vb.net and thought someone could get helped ..

    here is my code:

    Private Sub sPrintStatus(strTextToPrint As String, Optional boolIsNewLine As Boolean = False)
        REM intLastLength is declared as public variable on global scope like below
        REM intLastLength As Integer
        If boolIsNewLine = True Then
            intLastLength = 0
        End If
        If intLastLength > strTextToPrint.Length Then
            Console.Write(Convert.ToChar(13) & strTextToPrint.PadRight(strTextToPrint.Length + (intLastLength - strTextToPrint.Length), Convert.ToChar(" ")))
        Else
            Console.Write(Convert.ToChar(13) & strTextToPrint)
        End If
        intLastLength = strTextToPrint.Length
    End Sub
    

提交回复
热议问题