11

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>"

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
Mark Robbins
  • 2,427
  • 3
  • 24
  • 33

2 Answers2

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
  • 9
    This 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
    Can't find `SendKeys`. – Shimmy Weitzhandler Mar 01 '15 at 01:22
  • SendKeys is not contains in Console – nim Apr 17 '15 at 14:28
  • @nim using System.Windows.Forms; and it should work. – Zeuthenjr Apr 07 '17 at 11:03
  • 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