2017-12-27 2 views
0

C# .NET 콘솔 응용 프로그램에 대한 간단한 화살표 구동 옵션 메뉴를 작성하려고합니다. 문자 반환 유형에 대한 옵션 사전이 있으면 사용자는 위/아래 키를 사용하여 옵션을 선택하고 Enter를 사용하여 옵션을 선택합니다.C# Console Menu : 단일 실행 후 루프 숨기기

문제는 switch 문이 처음 실행 된 후 프로그램에서 완전히 분리된다는 것입니다. 또한 클래스에 기록 된 그 비동기 코드 의 일부에서이 코드를 분리하기로 결정 난 전혀 사용 여부에


편집하려면 중단 점을 배치하고 그것을 알아내는 시도했습니다. 이유가 뭐든, 그게 비동기가 이 덤비는 것이 왜 더는 이 같은 동기화 작업에 필요한 기다리고 있습니다 없어, 알아 내야 이제 .. 의도 한대로 작동하므로 을 할 수 있습니다 ..


코드 :

static char GetUserInput(Dictionary<char, String> options, int indent = 1) { 

     int optionAreaTop = Console.CursorTop; 
     Console.ForegroundColor = ConsoleColor.Yellow; 

     // First option, makes sure it's set in yellow 
     bool fo = true; 

     foreach (String opt in options.Values) { 
      Console.WriteLine(opt.PadLeft(indent + opt.Length, '\t')); 
      if (fo) { Console.ForegroundColor = ConsoleColor.White; fo = false; } 
     } 


     return DoMenu(options, optionAreaTop); 

    } 

    static char DoMenu(Dictionary<char, String> options, int optionAreaTop = 0) { 
     int answerIndex = 0; 
     int currentAnswerTop = optionAreaTop; 
     int indent = 2; 
     while (true) { 

      ConsoleKeyInfo kin = Console.ReadKey(true); 
      ConsoleKey ki = kin.Key; 

      switch (ki) { 
       case ConsoleKey.UpArrow: 
        if (currentAnswerTop - 1 >= optionAreaTop) { 
         // Rewrite selection in white 
         WriteOptionLine(currentAnswerTop, indent, options.Values.ElementAt(answerIndex), ConsoleColor.White); 
         WriteOptionLine(currentAnswerTop - 1, indent, options.Values.ElementAt(answerIndex - 1), ConsoleColor.Yellow); 
         currentAnswerTop -= 1; 
         answerIndex -= 1; 
        } 
        break; 

       case ConsoleKey.DownArrow: 
        if (answerIndex + 1 < options.Count - 1) { 
         // Rewrite selection in white 
         WriteOptionLine(currentAnswerTop, indent, options.Values.ElementAt(answerIndex), ConsoleColor.White); 
         WriteOptionLine(currentAnswerTop + 1, indent, options.Values.ElementAt(answerIndex + 1), ConsoleColor.Yellow); 
         currentAnswerTop += 1; 
         answerIndex += 1; 
        } 

        break; 

       case ConsoleKey.Enter: 
        return options.Keys.ElementAt(answerIndex); 

       default: 
        // Retry 
        break; 
      } 
     } 
    } 

    static void WriteOptionLine(int position, int indent, String option, ConsoleColor color) { 
     Console.SetCursorPosition(0, position); 
     Console.ForegroundColor = color; 
     Console.WriteLine(option.PadLeft(indent + option.Length, '\t')); 
    } 

사용법 :

Dictionary<char, String> opts = new Dictionary<char, string>(); 
opts.Add('r', "[R]etry the Download"); 
opts.Add('m', "[M]anually add the file"); 
opts.Add('s', "[S]kip the file"); 

// GET_USER_INPUT 
char choice = GetUserInput(opts, 2); 

// DO WHATEVER 
+0

Enter 키를 가져와야합니다. – jdweng

+0

질문이 불완전한 것 같습니다. 어떻게 프로그램을 디버그했으며 무엇을 발견 했습니까? – vasek

+0

무엇이 문제인가, 약간을 명확히 할 수 있습니까? – Aria

답변

0

내 콘솔 응용 프로그램에서 테스트 한 것처럼 재 때문에 ArrowDownif 조건으로 선택할 수 없습니다 결코 마지막 옵션은이 같은 변경 될 수 있으므로, 하나를 제외하고 코드와 아무 문제가 없다 :

case ConsoleKey.DownArrow: 
      if (answerIndex + 1 <= options.Count - 1) 
      { 
       // Rewrite selection in white 
       WriteOptionLine(currentAnswerTop, indent, options.Values.ElementAt(answerIndex), ConsoleColor.White); 
       WriteOptionLine(currentAnswerTop + 1, indent, options.Values.ElementAt(answerIndex + 1), ConsoleColor.Yellow); 
       currentAnswerTop += 1; 
       answerIndex += 1; 
      } 
      break; 

if (answerIndex + 1 < options.Count - 1)해야 if (answerIndex + 1 <= options.Count - 1)

당신이 옵션을 선택한 것 return options.Keys.ElementAt(answerIndex);choice 변수에 의해 DoMenu에서 돌아되기 때문에 Enter 키 콘솔 응용 프로그램을 눌러 종료 될 것이 분명하다.

+0

빠른 디버깅에서 알아 차 렸습니다. 5시에 코딩하지 마세요. – RobotGryphon

+0

그래서 해결 되었습니까? – Aria