【问题标题】:How to put some text in C# console instead of user typing it in?如何在 C# 控制台中放置一些文本而不是用户输入?
【发布时间】:2016-12-17 10:14:11
【问题描述】:

在我的 C# 控制台应用程序中,我提示用户插入 IP 地址:

string strIpAddress;
Console.WriteLine("Type the IP Address:");
strIpAddress = Console.ReadLine();

输出如下所示:

我想将默认 IP 地址文本准备好在控制台上供用户查看,然后按 ENTER。如果默认 IP 无效,那么用户应该能够删除文本(带退格键),更正 IP 地址,然后按 ENTER。用户应该会看到如下内容:

我不知道该怎么做! ;-(

感谢您的任何建议。

【问题讨论】:

标签: c# .net console


【解决方案1】:

已编辑以允许用户编辑

Console.Write("Type the IP Address:\n");
SendKeys.SendWait("192.168.1.1"); //192.168.1.1 text will be editable :)
strIpAddress=Console.ReadLine();

这需要将System.Windows.Forms 添加到引用并添加命名空间。

【讨论】:

  • 好吧,默认值不能这样编辑。但它的优点是可能是唯一简单的半解决方案。
  • 好吧,我实际上是为了这样一个“默认”目的
  • 感谢您的回答亲爱的@user3598756,但用户不可编辑。这样我应该提示用户:if this is the IP address, press enter. if not, type it yourself and then press enter! ;-)
  • 查看我从here获取的编辑后答案
  • 嗯,这太有趣了!
【解决方案2】:

使用Console.SetCursorPosition() 将光标向左移动(如果可能)和Console.ReadKey() 直接读取键以拦截退格键和输入键的更复杂示例:

using System;
using System.Linq;

namespace StackoverflowTests
{
    class Program
    {

        public static void Main(string[] args)
        {
            Console.WriteLine("Type the IP Address: ");
            //Put the default IP address 
            var defaultIP = "192.168.0.190";
            Console.Write(defaultIP);

            string input = defaultIP;
            //Loop through all the keys until an enter key
            while (true)
            {
                //read a key
                var key = Console.ReadKey(true);
                //Was this is a newline? 
                if (key.Key == ConsoleKey.Enter)
                {
                    Console.WriteLine();
                    break;
                }
                //Was is a backspace? 
                else if (key.Key == ConsoleKey.Backspace)
                {
                    //Did we delete too much?
                    if (Console.CursorLeft == 0)
                        continue; //suppress
                    //Put the cursor on character back
                    Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                    //Delete it with a space
                    Console.Write(" ");
                    //Put it back again
                    Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                    //Delete the last char of the input
                    input = string.Join("", input.Take(input.Length - 1));
                }
                //Regular key? add it to the input
                else if(char.IsLetterOrDigit(key.KeyChar))
                {
                    input += key.KeyChar.ToString();
                    Console.Write(key.KeyChar);
                } //else it must be another control code (ESC etc) or something.
            }

            Console.WriteLine("You entered: " + input);
            Console.ReadLine();
        }
    }
}

如果您想添加对LeftArrowRightArrow 按键的支持,甚至可以通过UpArrow 按键来回忆最后输入的内容,可以做得更加复杂。

【讨论】:

  • 这大概就是这么“简单”了。
  • 感谢@Maximilian,我已经测试了您的代码,它运行良好。我稍后应该添加的唯一内容是避免使用“ArrowUp”或“ESC”等非字符键击。虽然我在 .NET 控制台库中寻找一些我不知道的预定义方法!
  • @Shamim 你可以添加一个else if(char.IsLetterOrDigit(key.KeyChar)) 来获取这些键,我一开始不这样做是很糟糕的。
  • 没关系,但为了简单起见,我想我会使用 user3598756 的答案!
猜你喜欢
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多