Console.ReadLine with default text?

M

Martin Hart

Hi:

Is there a way I can use Console.ReadLine with some default text so that
if the user presses Enter the default text is returned?

TIA,
Martin.
 
P

Peter Duniho

Martin said:
Hi:

Is there a way I can use Console.ReadLine with some default text so that
if the user presses Enter the default text is returned?

No. But it should be trivial for you to compare the return value to an
empty string and provide your own default in that case. You could even
write your own helper method to do that, if you expect to have to do the
same thing in several places. For example:

string ReadLineWithDefault(string strDefault)
{
string strRead = Console.ReadLine();

if (strRead != "")
{
return strRead;
}

return strDefault;
}

Pete
 
M

Martin Hart

Pete:

Yes, and no...

What I was after, was to put the default string into the read buffer,
allowing the user to edit the string, and when they press Enter use the
(possibly) edited value.

This is not the same as comparing to an empty string as you will appreciate.

Any further ideas? Anyone?

Martin.


Peter Duniho escribió:
 
L

Lloyd Dupont

Maybe a Console.Write()
(as opposed to WriteLine() , I suggested only Write())

Also look at the other Console class member, you've got to use them for your
problem...

--
Regards,
Lloyd Dupont
NovaMind Software
Mind Mapping at its best
www.nova-mind.com
 
P

Peter Duniho

Lloyd said:
Maybe a Console.Write()
(as opposed to WriteLine() , I suggested only Write())

There are certainly ways to emulate the behavior. IMHO, the thing to
keep in mind though, is that the console itself does some handling of
user input, including maintaining a buffer of previously entered lines
and knowing the difference between text on the screen that the user
entered versus text on the screen displayed by the application.

I haven't played around with it enough to see how writing something
explicit to, for example, display the default text, handling
backspacing, etc. if the user wants to enter new text and providing a
default if they don't, would interact with the existing console input
behavior. But I'm not sure it would work well.

This would be a pretty easy thing to do in a Windows application that
has a "console" window completely implemented by the application. But
in an actual console application, with a real console window?

I'm not sure that there _is_ a straightforward reliable way to do what
the OP asked for.

Pete
 
M

Martin Hart

Pete:

Thanks for your interest and insight. I can see that this was not
'intended behavior' and as such, will steer my efforts in another direction.

Thanks to all for the help,
Martin.

Peter Duniho escribió:
 
A

Amry

You can try this:

Console.Write("Name: ");
System.Windows.Forms.SendKeys.SendWait("Amry");
string name = Console.ReadLine();
 

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