我的代码出现问题。
我正在尝试在菜单屏幕上选项的任一侧闪烁图标,以表示用户当前的选择。问题是我希望能够同时闪烁光标并读取用户正在输入的键移动光标的位置。有没有办法可以做到这一点,或者比我目前的设置更简单。
任何帮助都非常感谢,因为我还在学习,谢谢。
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
答案 0 :(得分:0)
除非您将功能错误地拆开以使其基本上可以在单个循环中运行,否则您需要在单独的线程中运行每个循环。一个复杂的主题,但值得学习:
http://msdn.microsoft.com/en-us/library/eed6swsx(v=vs.80).aspx
答案 1 :(得分:0)
尽管多线程可能是Brad所说的前进方向,但您可以通过 DoEvent 方法实现您想要的目标,该方法告诉系统在继续之前处理其他事件。< / p>
尝试在
之前粘贴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"