如何在.Net Console App中设置默认输入值?
如何在 .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);
当然,我不希望它这么简单。我打赌必须做一些低级的、不受管理的事情;我只是不知道怎么办。
编辑
我知道我可以用默认值替换没有输入。我要问的不是这个。我正在尝试了解实现我所描述的行为所涉及的内容:为用户提供可编辑的默认值。我也不担心输入验证;我的问题与此无关。
How can you set a default input value in a .net console app?
Here is some make-believe code:
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);
Of course, I don't expect it to be this simple. I am betting on having to do some low-level, unmanaged stuff; I just don't know how.
EDIT
I know I can replace no input with the default. That's not what I am asking about. I am trying to LEARN what's involved in achieving the behavior I described: giving the user an editable default. I'm also not worried about input validation; my question has nothing to do with that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我找到了一个完全符合您想要的解决方案。感谢 LagDaemon.ReadLine,它是Lunster 的回答中使用的包,我们可以这样做:
展示:
GIF 显示如何使用此答案
此解决方案还支持 快捷方式,例如左右箭头,用于移动光标并在到达时处理文本溢出终端的右边框。
警告:此解决方案不会替换/扩展官方
Console.ReadLine
方法,仍应被视为“hacky”。I have found a solution that does exactly what you want. Thanks to LagDaemon.ReadLine, which is a fork of package used in Lunster's answer, we can do this:
Showcase:
GIF showing how you can use this answer
This solution also supports shortcuts like left and right arrow to move the cursor and handles text overflow when you reach the terminal's right border.
Warning: this solution does not replace/extend the official
Console.ReadLine
method and should still be considered as "hacky".我相信您将通过监听每个按键来手动管理此操作:
快速组合示例:
这非常复杂 - 您必须确保光标不会超出范围并手动更新缓冲区。
I believe that you will have manage this manually by listening to each key press:
Quickly thown together example:
This is quite involved - you'll have to keep ensure the cursor doesn't go out of range and manually update your buffer.
这是一个简单的解决方案:
但是它并不完整。 SendWait 输入字符串中的某些字符具有特殊含义,因此您必须对它们进行转义(例如 +、(、) 等)
请参阅:http://msdn.microsoft.com/en -us/library/system.windows.forms.sendkeys.aspx 获取完整说明。
Here's a simple solution:
It's not complete however. Some characters in the SendWait input string have special meaning so you have to escape them (eg. +, (, ), etc.)
See: http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx for a complete description.
我继续完成了 Matt 的实现方法:
注意:
caret
参数),I went ahead and completed Matt's implementation approach:
Notes:
caret
parameter)或者...只需测试输入的值,如果为空,则将默认值放入输入中。
Or... Just test the value entered, if it's empty put the default value in input.
现在有更好的方法可以做到这一点,请查看 nuget 上的 Readline:https ://www.nuget.org/packages/ReadLine
install-package Readline
var input = ReadLine.Read("输入每周费用:", "135");< /code>
我喜欢使用控制台来编写交互式测试,并且使用默认值确实很有帮助。
There's a much better way to do this now, check out Readline on nuget: https://www.nuget.org/packages/ReadLine
install-package Readline
var input = ReadLine.Read("Enter weekly cost: ", "135");
I like to use the console to write interactive tests, and having default values can really help things.
)
您可以使用这样的辅助方法:
You can use helper method like this:
简单的解决方案,如果用户没有输入任何内容,则分配默认值:
在处理用户输入时,您应该预料到它可能包含错误。因此,如果用户未输入数字,您可以使用 TryParse 以避免异常:
这将被认为是处理用户输入的最佳实践。如果您需要解析许多用户输入,请使用辅助函数。一种方法是使用可空值的方法,如果解析失败则返回 null。然后使用 空合并运算符:
然后,读取输入并分配默认值就很简单:
Simple solution, if user inputs nothing, assign the default:
When dealing with user inputs, you should expect that it might contain errors. So you could use TryParse in order to avoid an exception, if the user has not input a number:
This would be considered best-practice for handling user input. If you need to parse many user inputs, use a helper function for that. One way of doing it is to use a method with a nullable and return null if parsing failed. Then it is very easy to assign a default value using the null coalescing operator:
Then, to read an input and assign a default value is as easy as: