Is there way of achieving this? I want to pass some text and have it appear on the input line -- instead of "Enter your Name:<cursor>", I want "Enter your Name:Default Editable Text<cursor>"
Asked
Active
Viewed 8,957 times
11
-
1This may be a duplicate: http://stackoverflow.com/q/1655318/1108263 – Brian Snow Jan 22 '12 at 16:51
-
1I think it's moreso a duplicate of http://stackoverflow.com/questions/7565415/edit-text-in-c-sharp-console-application – Slugart Jan 22 '12 at 16:56
-
This question hasn't solution.... :( – nim Apr 17 '15 at 14:40
2 Answers
16
Ok, found it. Sorry.
static void Main(string[] args)
{
Console.Write("Your editable text:");
SendKeys.SendWait("hello"); //hello text will be editable :)
Console.ReadLine();
}

Mark Robbins
- 2,427
- 3
- 24
- 33
-
9This will have interesting side-effects when your console window doesn't have the focus. – Hans Passant Jan 22 '12 at 18:18
-
Yes, but I am only doing this immediately following dos line instigation of my program. The program (right now at least) is interactive from the command line and retains session info between calls. Eventually it will not bounce back into dos. – Mark Robbins Jan 23 '12 at 03:04
-
1
-
-
-
In my case, simply adding a `using` to the class didn't work; received [this error](https://stackoverflow.com/questions/6639468/c-forms-does-not-exist-in-the-namespace-system-windows/6639482). You may need to add a reference by right-clicking on **References** in Solution Explorer, clicking **Add Reference...**, and checking **System.Windows.Forms** under **Assemblies > Framework**. – David Mancini Sep 21 '17 at 19:27
4
Assign the default value to your string and replace it only if the user has entered something.
Dim name, s As String
name = "John"
Console.Write($"Enter your Name (hit <Enter> for ""{name}""): ")
s = Console.ReadLine()
If Trim(s) <> "" Then
name = s
End If
Console.WriteLine("Result = {0}", name)
Console.ReadKey()

Olivier Jacot-Descombes
- 104,806
- 13
- 138
- 188