Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
I have a code where I can write text in a console character-by-character. The problem is, whenever I do backspace, it writes over the previous character/(s), instead of deleting the character and just rewriting the changed character. It also gave a strange symbol in the output instead of a backspace and then the change made. The symbol was: ◘. I thought of doing line-by-line, but what's the fun in that?
Anyway here is what I have got/tried to use.
VB
Imports System.Console
Module Window

    Sub Main()
        Title = "MSEdt32"
        Dim Str32 As String = ""
        BackgroundColor = ConsoleColor.Blue
        ForegroundColor = ConsoleColor.Yellow
        WriteLine("Welcome to MSEdt32 - Press F1 To Quit")
        ResetColor()
        Dim In32 As ConsoleKeyInfo
        Dim Ap32 As Char
        Do
            In32 = Console.ReadKey
            If In32.Key = ConsoleKey.Enter Then
                Console.WriteLine("")
            End If
            Ap32 = In32.KeyChar
            Str32 &= Ap32
        Loop While In32.Key <> ConsoleKey.F1
    End Sub

End Module

So, could anyone give a suggestion as how to do this? IDEA: I had a thought that every time a backspace occurs, the console could clear, trim the end (* how many backspaces) of the string, echo the string and continue - but I do not know if that is an OK method.

Any help would be appreciated,
-iProgramIt
Posted
Updated 7-Jun-15 4:24am
v3

Hi! Thanks to Matthew Soji I got it solved. Here is my final code for whoever wants it!
VB
On Error Resume Next
        Dim Title As String = "MSEdt32"
        Dim Str32 As String = ""
        BackgroundColor = ConsoleColor.Blue
        ForegroundColor = ConsoleColor.Yellow
        WriteLine("Welcome to MSEdt32 - Press F1 To Quit")
        ResetColor()
        Dim In32 As ConsoleKeyInfo
        Dim Ap32 As Char
        Do
            In32 = Console.ReadKey
            If In32.Key = ConsoleKey.Enter Then
                Console.WriteLine("")
            End If
            If In32.Key = ConsoleKey.Backspace Then
                Console.Write(" ")
                Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop)
                Str32 = Str32.Replace("", "")
                Str32 = Str32.Remove(Str32.Length - 1, 1)
            End If
            Ap32 = In32.KeyChar
            Str32 &= Ap32
        Loop While In32.Key <> ConsoleKey.F1
        Str32 = Str32.Replace("", "")
        MsgBox(Str32)

Cheers,
iProgramIt
 
Share this answer
 
try the below code

VB
Sub Main()
    Dim Title As String = "MSEdt32"
    Dim Str32 As String = ""
    BackgroundColor = ConsoleColor.Blue
    ForegroundColor = ConsoleColor.Yellow
    WriteLine("Welcome to MSEdt32 - Press F1 To Quit")
    ResetColor()
    Dim In32 As ConsoleKeyInfo
    Dim Ap32 As Char
    Do
        In32 = Console.ReadKey
        If In32.Key = ConsoleKey.Enter Then
            Console.WriteLine("")
        End If
        'Add the below code to handle backspace
        If In32.Key = ConsoleKey.Backspace Then
            Console.Write(" ")
            Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop)
        End If

        Ap32 = In32.KeyChar
        Str32 &= Ap32
    Loop While In32.Key <> ConsoleKey.F1
End Sub
 
Share this answer
 
Comments
iProgramIt 8-Jun-15 17:55pm    
Thanks - I changed it so now it will do it properly!
Cheers,
iProgramIt!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900