Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

Console.ReadLine with default text?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Hart - 27 Sep 2007 17:05 GMT
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.
Peter Duniho - 27 Sep 2007 18:33 GMT
> 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
Martin Hart - 27 Sep 2007 18:42 GMT
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ó:
>> Hi:
>>
[quoted text clipped - 19 lines]
>
> Pete
Lloyd Dupont - 28 Sep 2007 05:05 GMT
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...

Signature

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

> Pete:
>
[quoted text clipped - 35 lines]
>>
>> Pete
Peter Duniho - 28 Sep 2007 07:46 GMT
> 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
Martin Hart - 28 Sep 2007 08:38 GMT
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ó:
>> Maybe a Console.Write()
>> (as opposed to WriteLine() , I suggested only Write())
[quoted text clipped - 19 lines]
>
> Pete
Amry - 28 Sep 2007 13:24 GMT
You can try this:

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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.