在C#控制台应用程序中编辑文本? [重复]

时间:2022-09-18 20:47:45

This question already has an answer here:

这个问题在这里已有答案:

Is there a way to edit text in a C# console application? In other words, is it possible to place pre-defined text on the command line so that the user can modify the text and then re-submit it to the app?

有没有办法在C#控制台应用程序中编辑文本?换句话说,是否可以在命令行上放置预定义的文本,以便用户可以修改文本然后将其重新提交给应用程序?

2 个解决方案

#1


12  

One thing that came to my mind is to...simulate keystrokes. And a simple example using SendKeys:

我想到的一件事是......模拟击键。还有一个使用SendKeys的简单示例:

static void Main(string[] args)
{
    Console.Write("Your editable text:");
    SendKeys.SendWait("hello"); //hello text will be editable :)
    Console.ReadLine();
}

NOTE: This works only on active window.

注意:这仅适用于活动窗口。

#2


16  

Yes. You need to use method SetCursorPosition of Console. Example:

是。您需要使用Console的方法SetCursorPosition。例:

    Console.WriteLine("hello");
    Console.SetCursorPosition(4, 0);
    Console.WriteLine("      ");

It will display 'hell' You need custom realization of ReadLine method which let you to edit n-symbols (default string) in Console and return string from a user. This is my example:

它将显示'hell'你需要自定义ReadLine方法的实现,它允许你在Console中编辑n符号(默认字符串)并从用户返回字符串。这是我的例子:

static string ReadLine(string Default)
{
    int pos = Console.CursorLeft;
    Console.Write(Default);
    ConsoleKeyInfo info;
    List<char> chars = new List<char> ();
    if (string.IsNullOrEmpty(Default) == false) {
        chars.AddRange(Default.ToCharArray());
    }

    while (true)
    {
        info = Console.ReadKey(true);
        if (info.Key == ConsoleKey.Backspace && Console.CursorLeft > pos)
        {
            chars.RemoveAt(chars.Count - 1);
            Console.CursorLeft -= 1;
            Console.Write(' ');
            Console.CursorLeft -= 1;

        }
        else if (info.Key == ConsoleKey.Enter) { Console.Write(Environment.NewLine); break; }
        //Here you need create own checking of symbols
        else if (char.IsLetterOrDigit(info.KeyChar))
        {
            Console.Write(info.KeyChar);
            chars.Add(info.KeyChar);
        }
    }
    return new string(chars.ToArray ());
}

This method will display string Default. Hope I understood your problem right (I doubt in it)

此方法将显示字符串Default。希望我理解你的问题(我怀疑它)

#1


12  

One thing that came to my mind is to...simulate keystrokes. And a simple example using SendKeys:

我想到的一件事是......模拟击键。还有一个使用SendKeys的简单示例:

static void Main(string[] args)
{
    Console.Write("Your editable text:");
    SendKeys.SendWait("hello"); //hello text will be editable :)
    Console.ReadLine();
}

NOTE: This works only on active window.

注意:这仅适用于活动窗口。

#2


16  

Yes. You need to use method SetCursorPosition of Console. Example:

是。您需要使用Console的方法SetCursorPosition。例:

    Console.WriteLine("hello");
    Console.SetCursorPosition(4, 0);
    Console.WriteLine("      ");

It will display 'hell' You need custom realization of ReadLine method which let you to edit n-symbols (default string) in Console and return string from a user. This is my example:

它将显示'hell'你需要自定义ReadLine方法的实现,它允许你在Console中编辑n符号(默认字符串)并从用户返回字符串。这是我的例子:

static string ReadLine(string Default)
{
    int pos = Console.CursorLeft;
    Console.Write(Default);
    ConsoleKeyInfo info;
    List<char> chars = new List<char> ();
    if (string.IsNullOrEmpty(Default) == false) {
        chars.AddRange(Default.ToCharArray());
    }

    while (true)
    {
        info = Console.ReadKey(true);
        if (info.Key == ConsoleKey.Backspace && Console.CursorLeft > pos)
        {
            chars.RemoveAt(chars.Count - 1);
            Console.CursorLeft -= 1;
            Console.Write(' ');
            Console.CursorLeft -= 1;

        }
        else if (info.Key == ConsoleKey.Enter) { Console.Write(Environment.NewLine); break; }
        //Here you need create own checking of symbols
        else if (char.IsLetterOrDigit(info.KeyChar))
        {
            Console.Write(info.KeyChar);
            chars.Add(info.KeyChar);
        }
    }
    return new string(chars.ToArray ());
}

This method will display string Default. Hope I understood your problem right (I doubt in it)

此方法将显示字符串Default。希望我理解你的问题(我怀疑它)