如何正确地将char插入字符串?

时间:2019-05-18 20:42:13

标签: vb.net

我正在Visual Basic .NET中对文本编辑器进行编码,但遇到一些问题。我该如何解决?

我希望它能正常工作,但事实并非如此。而是输出错误。 基本上,我不断收到错误异常,告诉我这一点:

  

“类型为'System.NullReferenceException'的未处理异常   发生在CodeEditor.exe

     

其他信息:对象引用未设置为的实例   对象。”

Imports System.Windows.Forms

Module CLI_Editor

Public Lines As New List(Of String)
Public Line As Integer = 0
Public LinesNum As Integer = 0
Public TextWritten As String
Public LinesWritten As New List(Of String)
Public File As String

Public LastChar As Char

Public Position As New Integer
Sub Main()

    Do
        Dim Key As ConsoleKeyInfo = Console.ReadKey(True)

        If (Key.Key = ConsoleKey.UpArrow) Then
            If (Not (Line = 0)) Then
                Line -= 1
                Console.SetCursorPosition(LinesWritten.Item(Line).Length, Console.CursorTop - 1)
            End If

        ElseIf (Key.Key = ConsoleKey.DownArrow) Then
            Try
                Console.SetCursorPosition(LinesWritten.Item(Line + 1).Length, Console.CursorTop + 1)
                Line += 1
                LinesNum += 1
            Catch ex As Exception

            End Try

        ElseIf (Key.Key = ConsoleKey.LeftArrow) Then
            If (Not Console.CursorLeft = 0) Then
                Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop)
                Position = Console.CursorLeft
            End If

        ElseIf (Key.Key = ConsoleKey.RightArrow) Then
            Try
                Console.SetCursorPosition(Console.CursorLeft + 1, Console.CursorTop)
                Position = Console.CursorLeft
            Catch ex As Exception

            End Try



        ElseIf (Key.Key = ConsoleKey.Enter) Then
            Console.ResetColor()
            Line += 1
            LinesNum += 1
            LinesWritten.Add("")
            TextWritten = ""
            Console.SetCursorPosition(LinesWritten.Item(Line).Length, Console.CursorTop + 1)

        ElseIf (Key.Key = ConsoleKey.Backspace) Then
            Select Case TextWritten = ""
                Case False
                    If (Console.CursorLeft <> 0) Then
                        Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop)
                        Console.Write(" ")
                        Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop)
                        TextWritten = TextWritten.Remove(TextWritten.Length - 1, 1)
                        LinesWritten.Item(Line) = TextWritten
                    End If

                Case True
                    If (Not Console.CursorTop = 0) Then
                        Line -= 1
                        Console.SetCursorPosition(LinesWritten.Item(Line).Length, Console.CursorTop - 1)
                        LinesWritten.RemoveAt(Line + 1)
                        TextWritten = LinesWritten.Item(Line)
                    End If

            End Select

        ElseIf (Key.Key = ConsoleKey.F4) Then
            Dim full As String = ""
            For Each LineStr In LinesWritten
                full &= LineStr & vbNewLine
            Next
            MsgBox(full)
        Else
            ' Error occurs just below
            TextWritten = TextWritten.Insert(Console.CursorLeft, Key.KeyChar)

            Try
                LinesWritten.Item(Line) = TextWritten
            Catch ex As Exception
                LinesWritten.Add("")
                LinesWritten.Item(Line) = TextWritten
            End Try

            Console.Write(Key.KeyChar)
            LastChar = Key.KeyChar
        End If
    Loop
End Sub
End Module

亲爱的成员,谢谢您!

0 个答案:

没有答案