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?
Asked
Active
Viewed 1.4k times
19

Patrick Hofman
- 153,850
- 22
- 249
- 325

CatDadCode
- 58,507
- 61
- 212
- 318
-
6I do not believe my question is a duplicate of the provided question. My need is much more specific and the answer given on that question does not answer this question. The Console class, as far as I can tell, does not have anything to aid in placing editable text on the command line. – CatDadCode Sep 27 '11 at 07:27
-
1There is no command line in a console application unless you program one. And that can be done with the Console class. Edit: cmd line is the shell, what you use to start the console application. While it is running and you are outputting stuff to the console window you are not using shell commands i.e. the command line. You would have interpret the key(s) that have been pressed and perform specific actions depending on key (i.e. move cursor back one char when backspace is pressed).´ – Sascha Hennig Sep 27 '11 at 07:46
-
1@Alex Ford: Regarding "much more specific" - "how do I print int" is much more specific then "how do I print object", see no difference. – Andrey Agibalov Sep 27 '11 at 08:03
-
I'm not sure you're supposed to edit the text in the commandline... it IS a commandline after all :P – Jonas B Sep 27 '11 at 08:04
2 Answers
24
Yes. You need to use method SetCursorPosition of Console. Example:
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:
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)
-
2Can you clarify a little? I'm not sure I understand. I placed this code in a new console app and it does indeed display "hell" but how does this help me edit that text on the command line? – CatDadCode Sep 27 '11 at 07:31
-
2
-
Great answer. Very helpful for those who are having issues with SendKeys! – Matt Canty May 06 '14 at 16:42
15
One thing that came to my mind is to...simulate keystrokes. And a simple example using 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.

Renatas M.
- 11,694
- 1
- 43
- 62
-
2Your method is simpler than mine. It seems I like to invent bicycles :)) – Tadeusz Sep 27 '11 at 08:17
-
1Your code is really good and you not inventing bicycle because in my suggested solution need to use System.Windows.Forms namespace or PInvoke SendInput or something familiar. +1 from me :) – Renatas M. Sep 27 '11 at 08:26
-
-
1As pointed out in another question, you need to be careful that your app has focus. – Ray Feb 14 '12 at 15:19
-
2As above, do be careful with this answer. [SendKeys doesn't always work...](http://bytes.com/topic/visual-basic-net/answers/460372-sendkeys-doesnt-always-work). To avoid these problems review [Praetor12's](http://stackoverflow.com/a/7565586/966609|) answer. – Matt Canty May 06 '14 at 16:43
-
-
@nim please read second comment. Need to add reference to System.Windows.Forms – Renatas M. Apr 20 '15 at 11:27