如何在.Net控制台应用程序中设置默认输入值?如何在.net控制台应用程序中设置默认输入值?这是一些假代码:Console.Write("Enterweeklycost:");字符串输入=Console.ReadLine("135");//135是默认值。用户可以更改或按回车键接受十进制weeklyCost=decimal.Parse(input);当然,我不希望它这么简单。我将不得不做一些低级的、不受管理的事情;我只是不知道怎么办。编辑我知道我可以用默认值替换没有输入。这不是我的问题。我试图了解实现我所描述的行为所涉及的内容:为用户提供可编辑的默认值。我也不担心输入验证;我的问题与此无关。我相信您可以通过接收每个按键来手动管理:快速拼凑在一起的例子://writetheinitialbufferchar[]buffer="Initialtext".ToCharArray();控制台.WriteLine(缓冲区);//通过将光标向上移动一行来确保光标从文本行开始Console.SetCursorPosition(Console.CursorLeft+buffer.Length,Console.CursorTop-1);//循环处理按键操作,直到用户按下enter//(这可能需要更复杂一些——转义怎么样?)ConsoleKeyInfokeyInfo=Console.ReadKey(true);while(keyInfo.Key!=ConsoleKey.Enter){switch(keyInfo.Key){caseConsoleKey.LeftArrow:...//通过移动光标位置来处理左键//需要跟踪缓冲区中的位置//如果用户按下另一个键,则更新缓冲区中的文本//并在屏幕上绘制字符//有很多情况需要处理(退格、删除等)}keyInfo=Console.ReadKey(真的);}这非常复杂–您必须确保保护光标不会超出范围并手动更新缓冲区这是一个简单的解决方案:publicstaticstringConsoleReadLineWithDefault(stringdefaultValue){System.Windows.Forms.SendKeys.SendWait(defaultValue);返回Console.ReadLine();但这还不完整。SendWait输入字符串中的某些字符具有特殊含义,因此您必须对它们进行转义(例如+、(、)等)参见:http://msdn.microsoft.com/en-us/library/system。视窗。forms.sendkeys.aspx获取完整说明。或者...只是测试输入的值,如果它是空的,则将默认值放入输入中。将对程序集库“System.Windows.Forms”的引用添加到项目中在Console.WriteLine命令之后和Console.ReadLine命令之前添加SendKeys.SendWait("DefaultText")string_weeklycost="";Console.WriteLine("请输入每周费用:");System.Windows.Forms.SendKeys.SendWait("135");_weeklycost=Console.ReadLine();简单的解决方案,如果用户没有输入,则分配默认值:Console.Write("Enterweeklycost:");字符串输入=Console.ReadLine();十进制weeklyCost=String.IsNullOrEmpty(input)?135:decimal.Parse(输入);处理用户输入时,您应该预料到它可能包含错误。因此,如果用户未输入数字,您可以使用TryParse避免异常:Console.Write("Enterweeklycost:");字符串输入=Console.ReadLine();十进制每周费用;如果(!Decimal.TryParse(input,outweeklyCost))weeklyCost=135;这被认为是处理用户输入的最佳实践。如果您需要解析大量用户输入,请使用辅助函数。一种方法是使用可空方法,如果解析失败则返回null。然后很容易使用空合并运算符分配默认值:publicstaticclassSafeConvert{publicstaticdecimal?ToDecimal(字符串值){十进制d;如果(!Decimal.TryParse(value,outd))返回null;返回d;然后,读取输入并分配默认值就像:decimald=SafeConvert.ToDecimal(Console.ReadLine())??135;您可以使用这样的辅助方法:publicstaticstringReadWithDefaults(stringdefaultValue){stringstr=Console.ReadLine();返回String.IsNullOrEmpty(str)?默认值:str;我继续Matt的实现:publicstaticstringReadInputWithDefault(stringdefaultValue,stringcaret=">"){Console.WriteLine();//确保我们在新的一行Listbuffer=defaultValue.ToCharArray().Take(Console.WindowWidth-caret.Length-1).ToList();控制台。写入(插入符号);Console.Write(缓冲区.ToArray());Console.SetCursorPosition(Console.CursorLeft,Console.CursorTop);ConsoleKeyInfokeyInfo=Console.ReadKey(true);while(keyInfo.Key!=ConsoleKey.Enter){switch(keyInfo.Key){caseConsoleKey.LeftArrow:Console.SetCursorPosition(Math.Max(Console.CursorLeft-1,caret.Length),Console.CursorTop);休息;案例ConsoleKey.RightArrow:Console.SetCursorPosition(Math.Min(Console.CursorLeft+1,caret.Length+buffer.Count),Console.CursorTop);休息;案例ConsoleKey.Home:Console.SetCursorPosition(caret.Length,Console.CursorTop);休息;caseConsoleKey.End:Console.SetCursorPosition(caret.Length+buffer.Count,Console.CursorTop);休息;caseConsoleKey.Backspace:if(Console.CursorLeft=caret.Length+buffer.Count){中断;}varcursorColumnAfterDelete=Console.CursorLeft;buffer.RemoveAt(Console.CursorLeft-caret.Length);RewriteLine(插入符,缓冲区);Console.SetCursorPosition(cursorColumnAfterDelete,Console.CursorTop);休息;默认值:varcharacter=keyInfo.KeyChar;如果(字符Console.WindowWidth||caret.Length+buffer.Count>=Console.WindowWidth-1){break;//目前只支持一行输入}buffer.Insert(Console.CursorLeft-caret.长度、字符);RewriteLine(插入符,缓冲区);Console.SetCursorPosition(cursorAfterNewChar,Console.CursorTop);休息;}keyInfo=Console.ReadKey(true);}Console.Write(Environment.NewLine);返回新字符串(缓冲区.ToArray());}privatestaticvoidRewriteLine(stringcaret,Listbuffer){Console.SetCursorPosition(0,Console.CursorTop);Console.Write(newstring('',Console.WindowWidth-1));安慰。SetCursorPosition(0,Console.CursorTop);控制台。写入(插入符号);Console.Write(buffer.ToArray());}注:以上为C#学习教程:如何在.Net控制台应用程序中设置默认输入值?分享的所有内容,如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
