2012-12-11 25 views
1

我即將開始一個項目,這裏是我目前的歡迎屏幕。居中readline語句

string[] welcome = new string[4] 
{ 
    "Welcome", 
    "Choose A Option Bellow By Inputing The Number And Clicking Enter.", 
    "1. View C:\\Windows\\ File directory", 
    "2. View Your Own Custom Directory" 
}; 

string userChoice; 
for (int x = 0; x < 4; x++) 
{ 
    Console.WriteLine(
     "{0," + ((Console.WindowWidth/2) + welcome[x].Length/2) + "}", welcome[x]); 
} 

我該如何居中讀取的行?這樣當用戶選擇他們的選擇時也會居中呢?

+2

「並點擊Enter」 - >我很好奇你將如何處理:) – TweeZz

回答

5

您可以使用CursorLeftCursorTop屬性來移動光標。你有()之前

Console.CursorLeft = Console.WindowWidth/2; 
// maybe -1 to center the typed number correctly 
0

我相信你應該使用.WRITE和格式:
所以,你的情況,你會做這樣的事情。然後,添加一個Readline/ReadKey後:

  string[] welcome = new string[4] { "Welcome", "Choose A Option Bellow By Inputing The Number And Clicking Enter.", "1. View C:\\Windows\\ File directory", "2. View Your Own Custom Directory" }; 
     String userChoice; 
     for (int x = 0; x < 4; x++) 
     { 
      Console.WriteLine("{0," + ((Console.WindowWidth/2) + welcome[x].Length/2) + "}", welcome[x]); 
     } 
     var cprompt = "Choice:"; 
     Console.Write(string.Format("{0," + ((Console.WindowWidth/2) + (cprompt.Length/2)) + "}", cprompt)); 
     Console.ReadLine();