gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:18:38 24 4
gpt4 key购买 nike

如何在 .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);

当然,我没想到会这么简单。我打赌必须做一些低级的、不受管理的事情;我只是不知道如何。

编辑

我知道我可以用默认值替换无输入。那不是我要问的。我正在尝试了解实现我描述的行为所涉及的内容:为用户提供可编辑的默认值。我也不担心输入验证;我的问题与此无关。

最佳答案

我相信您会通过聆听每次按键的声音来手动管理它:

快速组合示例:

   // 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);
}

这非常复杂 - 您必须确保光标不会超出范围并手动更新缓冲区。

关于c# - 如何在 .Net 控制台应用程序中设置默认输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1655318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com