2009-10-31 31 views
12

如何在.net控制台应用程序中设置默认输入值?如何在.Net控制台应用程序中设置默认输入值?

下面是一些虚幻的代码:

Console.Write("Enter weekly cost: "); 
string input = Console.ReadLine("135"); // 135 is the default. The user can change or press enter to accept 
decimal weeklyCost = decimal.Parse(input); 

当然,我不希望它是这么简单。我打赌不得不做一些低级的,不受管理的东西;我只是不知道如何。

EDIT

我知道我可以用默认值替换没有输入。这不是我所问的。我想了解实现我描述的行为所涉及的内容:给用户一个可编辑的默认值。我也不担心输入验证;我的问题与此无关。

+0

的答案建议你可以编写本 - 用户不会在意的编码TECHNIC。对于理论上的问题,是否有一种方法可以用readline来做 - 可能不会(至少没有记录)。 – Dani 2009-10-31 19:29:18

+0

但是 - 我看到你正在尝试去,我们正在寻找一种解决方案,使用户能够更改默认文本。 – Dani 2009-10-31 19:32:13

+0

我知道它不能用.ReadLine()来完成。但是,我知道有一种方法可以做到这一点。 – 2009-10-31 19:34:13

回答

5

我相信,你将不得不手动通过听取各按键管理此:

快速thown在一起,例如:

// write the initial buffer 
    char[] buffer = "Initial text".ToCharArray(); 
    Console.WriteLine(buffer); 

    // ensure the cursor starts off on the line of the text by moving it up one line 
    Console.SetCursorPosition(Console.CursorLeft + buffer.Length, Console.CursorTop - 1); 

    // process the key presses in a loop until the user presses enter 
    // (this might need to be a bit more sophisticated - what about escape?) 
    ConsoleKeyInfo keyInfo = Console.ReadKey(true); 
    while (keyInfo.Key != ConsoleKey.Enter) 
    { 

     switch (keyInfo.Key) 
     { 
      case ConsoleKey.LeftArrow: 
        ... 
       // process the left key by moving the cursor position 
       // need to keep track of the position in the buffer 

     // if the user presses another key then update the text in our buffer 
     // and draw the character on the screen 

     // there are lots of cases that would need to be processed (backspace, delete etc) 
     } 
     keyInfo = Console.ReadKey(true); 
    } 

这是相当复杂 - 你必须保持保证光标不出去的范围和手动更新您的缓冲区。

+0

我不认为这是问题的含义。 – driis 2009-10-31 19:54:04

+0

其实这绝对是迄今为止最好的答案。 – 2009-10-31 19:56:17

+0

将此投入到扩展方法中,以便您可以调用Console.ReadLine(“135”);扩展方法可以重载现有方法吗?如果没有,给它一个新的名字。 – CoderDennis 2009-10-31 20:00:51

3

或...只要测试输入的值,如果它是空的,将默认值放入输入。

+0

,使它看起来更好 - 你可以使用一个属性,并将其添加到setter中。... – Dani 2009-10-31 19:22:44

1

简单的解决方案,如果用户输入什么,将默认:

Console.Write("Enter weekly cost: "); 
string input = Console.ReadLine(); 
decimal weeklyCost = String.IsNullOrEmpty(input) ? 135 : decimal.Parse(input); 

当用户输入打交道时,你应该想到,它可能包含错误。所以,你可以使用的TryParse为了避免异常,如果用户没有输入一个数字:

Console.Write("Enter weekly cost: "); 
string input = Console.ReadLine(); 
decimal weeklyCost; 
if (!Decimal.TryParse(input, out weeklyCost)) 
    weeklyCost = 135; 

这将被认为是最佳实践,用于处理用户输入。如果您需要解析许多用户输入,请使用辅助函数。这样做的一种方法是使用可空的方法,如果解析失败,则返回null。然后,它是非常容易使用null coalescing operator指定一个默认值:

public static class SafeConvert 
{ 
    public static decimal? ToDecimal(string value) 
    { 
     decimal d; 
     if (!Decimal.TryParse(value, out d)) 
      return null; 
     return d; 
    } 
} 

然后,读取输入,并指定一个默认值一样简单:

decimal d = SafeConvert.ToDecimal(Console.ReadLine()) ?? 135; 
+0

您将他的虚构参数保留在ReadLine中。 – 2009-10-31 19:27:38

+0

@亚当,谢谢你指出,回答编辑。 – driis 2009-10-31 19:33:35

0

您可以像使用helper方法这样的:

public static string ReadWithDefaults(string defaultValue) 
{ 
    string str = Console.ReadLine(); 
    return String.IsNullOrEmpty(str) ? defaultValue : str; 
} 
6

这里有一个简单的解决方案:

public static string ConsoleReadLineWithDefault(string defaultValue) 
{ 
    System.Windows.Forms.SendKeys.SendWait(defaultValue); 
    return Console.ReadLine(); 
} 

它不但是完成。 SendWait输入字符串中的某些字符具有特殊含义,因此您必须将其转义(例如。+,(,)等)。 有关完整说明,请参阅:http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

2
  1. 添加引用到国会图书馆“System.Windows.Forms的”你的项目
  2. 您Console.WriteLine命令后,你的控制台前,立即加入SendKeys.SendWait(“DefaultText”)。的ReadLine命令

 

string _weeklycost = ""; 
Console.WriteLine("Enter weekly cost: "); 
System.Windows.Forms.SendKeys.SendWait("135"); 
_weeklycost = Console.ReadLine(); 
相关问题