Groups | Blog | Home
all groups > dotnet framework > september 2007 >

dotnet framework : Console.ReadLine with default text?


Peter Duniho
9/27/2007 10:33:40 AM
[quoted text, click to view]

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;
}

Martin Hart
9/27/2007 6:05:07 PM
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 Hart
9/27/2007 7:42:28 PM
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ó:
[quoted text, click to view]
Peter Duniho
9/27/2007 11:46:26 PM
[quoted text, click to view]

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.

Martin Hart
9/28/2007 12:00:00 AM
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ó:
[quoted text, click to view]
Lloyd Dupont
9/28/2007 12:00:00 AM
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
[quoted text, click to view]
Amry
9/28/2007 12:24:54 PM
You can try this:

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