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.
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.
I went ahead and completed Matt's implementation approach:
Notes:
caret
parameter)You can use helper method like this:
Or... Just test the value entered, if it's empty put the default value in input.
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.