1

我的代码有问题。

我正在尝试在菜单屏幕上的选项两侧设置闪烁的图标,以表示用户当前的选择。问题是我希望能够同时闪烁光标并读取用户输入移动光标位置的键。有没有办法可以做到这一点,或者比我目前拥有的设置更简单。

非常感谢任何帮助,因为我仍在学习,谢谢。

Sub Main()
    Dim KeyPressed As String
    Dim Pos As Integer
    Dim MenuSelection As Integer = 1
    PreStuff()
    DisplayMenu()
    Cursor(MenuSelection, False)
    Do
        KeyPressed = GetKeyPressed()
        If KeyPressed = "ConsoleKey.DownArrow" And Pos = 1 Then
            MenuSelection = 2
        ElseIf KeyPressed = "ConsoleKey.DownArrow" And Pos = 2 Then
            MenuSelection = 1
        ElseIf KeyPressed = "ConsoleKey.UpArrow" And Pos = 1 Then
            MenuSelection = 2
        ElseIf KeyPressed = "ConsoleKey.UpArrow" And Pos = 2 Then
            MenuSelection = 1
        End If
    Loop Until KeyPressed = "ConsoleKey.Enter"
    Cursor(0, True)
End Sub

Sub PreStuff()
    Console.Title = "Populatio - Pre Alpha"
    Console.SetWindowSize(80, 25)
    Console.CursorVisible = False
End Sub

Function GetKeyPressed()
    Dim KeyChoice As String
    KeyChoice = Console.ReadKey().ToString
    Return KeyChoice
End Function


Sub Cursor(ByVal Op As Integer, ByVal State As Boolean)
    Do
        Select Case Op
            Case 1 And State = False
                Console.SetCursorPosition(32, 5)
                Console.Write("►")
                Console.SetCursorPosition(43, 5)
                Console.Write("◄")
                State = True
            Case 1 And State = True
                Console.SetCursorPosition(32, 5)
                Console.Write(" ")
                Console.SetCursorPosition(43, 5)
                Console.Write(" ")
                State = False
            Case 2 And State = False
                Console.SetCursorPosition(34, 7)
                Console.Write("►")
                Console.SetCursorPosition(41, 7)
                Console.Write("◄")
                State = True
            Case 2 And State = True
                Console.SetCursorPosition(34, 7)
                Console.Write(" ")
                Console.SetCursorPosition(41, 7)
                Console.Write(" ")
                State = False
        End Select
        System.Threading.Thread.Sleep(500)
    Loop Until Op = 0
    Console.SetCursorPosition(32, 5)
    Console.Write(" ")
    Console.SetCursorPosition(43, 5)
    Console.Write(" ")
    Console.SetCursorPosition(34, 7)
    Console.Write(" ")
    Console.SetCursorPosition(41, 7)
    Console.Write(" ")
End Sub

Sub DisplayMenu()
    Console.WriteLine("╔═════════════════════════════════════════════════════════════════════════════╗")
    Console.WriteLine("║                            Populatio! By Alx                                ║")
    Console.WriteLine("╠═════════════════════════════════════════════════════════════════════════════╣")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                 New Game                                    ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                   Exit                                      ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.WriteLine("║                                                                             ║")
    Console.Write("╚═════════════════════════════════════════════════════════════════════════════╝")
End Sub
4

2 回答 2

0

除非您有问题地拆分功能以便它基本上可以在单个循环中运行 - 您需要在单独的线程中运行每个循环。一个复杂的主题,但值得学习:

http://msdn.microsoft.com/en-us/library/eed6swsx(v=vs.80).aspx

于 2013-03-27T17:29:46.783 回答
0

尽管正如 Brad 所说,多线程可能是前进的方向,但您也许可以使用DoEvent方法实现您想要的,该方法告诉系统在继续之前先处理其他事件。

尝试在之前粘贴 DoEvents

    Loop Until KeyPressed = "ConsoleKey.Enter"

这将允许 Cursor 方法再次运行,因此循环将变为...

Do
    KeyPressed = GetKeyPressed()
    If KeyPressed = "ConsoleKey.DownArrow" And Pos = 1 Then
        MenuSelection = 2
    ElseIf KeyPressed = "ConsoleKey.DownArrow" And Pos = 2 Then
        MenuSelection = 1
    ElseIf KeyPressed = "ConsoleKey.UpArrow" And Pos = 1 Then
        MenuSelection = 2
    ElseIf KeyPressed = "ConsoleKey.UpArrow" And Pos = 2 Then
        MenuSelection = 1
    End If
    DoEvents
Loop Until KeyPressed = "ConsoleKey.Enter"
于 2014-05-01T08:36:32.830 回答