如何在vb.net中的此代码中接受退格? [英] How do I accept backspace within this code in vb.net?

查看:130
本文介绍了如何在vb.net中的此代码中接受退格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个代码,我可以逐个字符地在控制台中写文本。问题是,每当我做退格键时,它都会写入前一个字符/(s),而不是删除字符,只是重写已更改的字符。它还在输出中给出了一个奇怪的符号,而不是退格然后所做的更改。符号是:◘。我想过一行一行,但有什么好玩的?

无论如何这里是我有/尝试使用的内容。

  Imports  System.Console 
模块窗口

Sub Main()
Title = MSEdt32
Dim Str32 作为 字符串 = < span class =code-string>
BackgroundColor = ConsoleColor.Blue
ForegroundColor = ConsoleColor.Yellow
WriteLine( 欢迎使用MSEdt32 - 按F1退出
ResetColor()
Dim In32 As ConsoleKeyInfo
Dim Ap32 As < span class =code-keyword> Char

In32 = Console.ReadKey
如果 In32.Key = ConsoleKey.Enter 然后
Console.WriteLine(
结束 如果
Ap32 = In32.KeyChar
Str32& = Ap32
循环 In32.Key<> ConsoleKey.F1
结束 Sub

结束 模块



所以,任何人都可以提出如何建议做这个? IDEA:我曾经想过,每次退格时,控制台都可以清除,修剪字符串的末尾(*多少退格),回显字符串并继续 - 但我不知道是否可以方法。



任何帮助都将不胜感激,

-iProgramIt

解决方案
嗨!感谢Matthew Soji,我解决了它。以下是我想要它的最终代码!

  On  错误 恢复 下一步 
Dim 标题作为 字符串 = MSEdt32
Dim Str32 As 字符串 =
BackgroundColor = ConsoleColor.Blue
ForegroundColor = ConsoleColor.Yellow
WriteLine( 欢迎光临MSEdt32 - 按F1退出
ResetColor()
Dim In32 As Conso leKeyInfo
Dim Ap32 As Char
执行
In32 = Console.ReadKey
如果 In32。 Key = ConsoleKey.Enter 然后
Console.WriteLine(
结束 如果
如果 In32.Key = ConsoleKey.Backspace 然后
Console.Write(
Console.SetCursorPosition(Console.CursorLeft - 1 ,Console.CursorTop)
Str32 = Str32.Replace(
Str32 = Str32 。删除(Str32.Length - 1 1
结束 如果
Ap32 = In32.KeyChar
Str32& = Ap32
循环 In32.Key<> ConsoleKey.F1
Str32 = Str32.Replace(
MsgBox(Str32)



干杯,

iProgramIt


尝试以下代码



 < span class =code-keyword> Sub  Main()
Dim 标题 As 字符串 = MSEdt32
Dim Str32 As String =
BackgroundColor = ConsoleColor.Blue
ForegroundColor = ConsoleColor.Yellow
WriteLine( 欢迎使用MSEdt32 - P ress F1退出
ResetColor()
Dim In32 As ConsoleKeyInfo
Dim Ap32 As Char
执行
In32 = Console.ReadKey
如果 In32.Key = ConsoleKey.Enter 然后
Console.WriteLine(
结束 如果
< span class =code-comment>' 添加以下代码以处理退格
如果 In32.Key = ConsoleKey.Backspace 然后
Console.Write( < span class =code-stri ng>)
Console.SetCursorPosition(Console.CursorLeft - 1 ,Console.CursorTop)
结束 如果

Ap32 = In32.KeyChar
Str32& = Ap32
循环 In32.Key<> ConsoleKey.F1
结束 Sub


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.

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

解决方案

Hi! Thanks to Matthew Soji I got it solved. Here is my final code for whoever wants it!

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


try the below code

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


这篇关于如何在vb.net中的此代码中接受退格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆