get "default" text from the console

P

per9000

Hi,
can I print some default text on the console when doing a ReadLine?

A silly example below shows two small scenarios. In the first a user
is told what he appears to be called, and asks for a name.

In the second case I want the the user to be able to just press return
to accept the username of the system. (I understand it does not work
the way I do it below - I just want to illustrate what I want).

Thanks,
P9K

---------
using System;
using System.Text;

namespace FeedMeName
{
class Program
{
static void Main(string[] args)
{
string name1 = Environment.UserName;
Console.WriteLine("Are you '{0}'?", name1);
Console.Write("name please >> ");
name1 = Console.ReadLine();
Console.WriteLine("Hello '{0}'!", name1);

string name2 = Environment.UserName;
Console.WriteLine("Are you '{0}'?.", name2);
Console.Write("name please >> ");
Console.Write(name2);
Console.CursorLeft -= name2.Length;
name2 = Console.ReadLine();
Console.WriteLine("Hello '{0}'!", name2);
}
}
}
---------
FeedMeName.exe
Are you 'per'?
name please >> my name is foobar
Hello 'my name is foobar'!
Are you 'per'?.
name please >> foobar, goddamit
Hello 'foobar, goddamit'!
---------
FeedMeName.exe
Are you 'per'?
name please >> fo
Hello 'fo'!
Are you 'per'?.
name please >> brr
Hello 'br'!
 
J

Jon Skeet [C# MVP]

per9000 said:
can I print some default text on the console when doing a ReadLine?

ReadLine will read data. If you want to write some text on the console
before reading, just call Write or WriteLine with that text, and handle
the case where the user just presses return (i.e. you'll get an empty
line back).
 
P

per9000

ReadLine will read data. If you want to write some text on the console
before reading, just call Write or WriteLine with that text, and handle
the case where the user just presses return (i.e. you'll get an empty
line back).

Sure, that would work fine if the user accepts the username or has a
short username. But I want to be able to treat the case when a user
wants to *change* a line. Imagine a user with a really long name like
"Boutros Boutros Boutros Boutros Boutros Boutros Boutros Boutros-
Ghali". Perhaps such a user would like to just press delete a couple
of times to get a name like "BBBBBBBB-G" . Or imagine that this is a
text-editor (like vi or nano) and a user wants to rewrite a line at a
time. If the only change to a line is a typo then it is too much to
ask of a user to rewrite the entire line.

Perhaps what I want is to write the line to standard in or something
like that. Something like this (illegal code but this is that I want):
string line = "hell_o_world() {Console.WriteLine("hell o
world") } ;"; // notice the incorrect ; in the string
Console.Write("Rewrite this line>>");
Console.In.Write(line);
line = Console.ReadLine();

/Per
 
J

Jon Skeet [C# MVP]

Sure, that would work fine if the user accepts the username or has a
short username. But I want to be able to treat the case when a user
wants to *change* a line.

I can't say I've seen console applications which work like that.
Normally it would put the default in square brackets, like this:

Enter administrative user name:
(User enters "Fred")
Enter helpdesk user name [Fred]:

At that point, hitting return would use the value "Fred".

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top