5

In my C# console application, I prompt user to insert the ip address:

string strIpAddress;
Console.WriteLine("Type the IP Address:");
strIpAddress = Console.ReadLine();

Output looks like this:

enter image description here

I want to put the default IP address text ready on console for user to see and just hit the ENTER. If the default IP is invalid then user should be able to delete the text (with backspace), correct the IP address, then hit the ENTER. User should see something like this:

enter image description here

I don't know how to do this! ;-(

Thanks for any sugestion.

Shamim
  • 434
  • 4
  • 11
  • 1
    I beleive it's a duplicate of: http://stackoverflow.com/questions/1655318/how-to-set-default-input-value-in-net-console-app – evilkos Dec 17 '16 at 10:20
  • The one idea that comes to mind is to hook the keyboard and simulate the typing yourself, but something tells me that would be overkill. – Abion47 Dec 17 '16 at 10:20
  • 4
    The normal, **easy** way is to simply let the user know what the default will be if he hits enter, something like `Type the IP Address [192.168.1.1]:`, everything else isn't really all that easy as you will have to simulate typing or build the editor yourself. – Lasse V. Karlsen Dec 17 '16 at 10:22
  • Maximilian Gerhardt's answer below is probably as simple as you're going to get with a purely C# method. If you want to try your hand at P/Invoke, there might be something you can use here: http://www.pinvoke.net/default.aspx/kernel32/ConsoleFunctions.html – Abion47 Dec 17 '16 at 10:29
  • Possible duplicate of [How to set default input value in .Net Console App?](https://stackoverflow.com/questions/1655318/how-to-set-default-input-value-in-net-console-app) – IvanH Aug 27 '18 at 09:46

2 Answers2

5

Edited to allow for user editing

Console.Write("Type the IP Address:\n");
SendKeys.SendWait("192.168.1.1"); //192.168.1.1 text will be editable :)
strIpAddress=Console.ReadLine();

This requires adding the System.Windows.Forms to the references and adding the namespace.

TaW
  • 53,122
  • 8
  • 69
  • 111
user3598756
  • 28,893
  • 4
  • 18
  • 28
  • 2
    Well, the default value won't be editable this way. But it has the advantage of probably being the only simple half-solution. – evilkos Dec 17 '16 at 10:22
  • well I actually intended that for such a "default" purpose – user3598756 Dec 17 '16 at 10:24
  • Thanks for your answer dear @user3598756, but it's not editable by user. this way i should prompt user: `if this is the IP address, press enter. if not, type it yourself and then press enter!` ;-) – Shamim Dec 17 '16 at 10:25
  • see edited answer I took from [here](http://stackoverflow.com/questions/7565415/edit-text-in-c-sharp-console-application/7565586#7565586) – user3598756 Dec 17 '16 at 10:40
  • Well, this is so interesting! – Shamim Dec 17 '16 at 10:51
  • Very nice, works just fine here..! Now it is the full solution and much simpler than any other suggestion.. – TaW Dec 17 '16 at 11:05
4

A more sophisticated example using Console.SetCursorPosition() to move the cursor to the left (if possible) and Console.ReadKey() to read the keys directly to intercept Backspace presses and enter keys:

using System;
using System.Linq;

namespace StackoverflowTests
{
    class Program
    {

        public static void Main(string[] args)
        {
            Console.WriteLine("Type the IP Address: ");
            //Put the default IP address 
            var defaultIP = "192.168.0.190";
            Console.Write(defaultIP);

            string input = defaultIP;
            //Loop through all the keys until an enter key
            while (true)
            {
                //read a key
                var key = Console.ReadKey(true);
                //Was this is a newline? 
                if (key.Key == ConsoleKey.Enter)
                {
                    Console.WriteLine();
                    break;
                }
                //Was is a backspace? 
                else if (key.Key == ConsoleKey.Backspace)
                {
                    //Did we delete too much?
                    if (Console.CursorLeft == 0)
                        continue; //suppress
                    //Put the cursor on character back
                    Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                    //Delete it with a space
                    Console.Write(" ");
                    //Put it back again
                    Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                    //Delete the last char of the input
                    input = string.Join("", input.Take(input.Length - 1));
                }
                //Regular key? add it to the input
                else if(char.IsLetterOrDigit(key.KeyChar))
                {
                    input += key.KeyChar.ToString();
                    Console.Write(key.KeyChar);
                } //else it must be another control code (ESC etc) or something.
            }

            Console.WriteLine("You entered: " + input);
            Console.ReadLine();
        }
    }
}

Can be made even more sophisticated if you want to add support for LeftArrow and RightArrow presses, or even UpArrow presses for recalling the last typed in stuff.

Maximilian Gerhardt
  • 5,188
  • 3
  • 28
  • 61
  • This is probably as "simple" as it gets. – Abion47 Dec 17 '16 at 10:27
  • Thanks @Maximilian, I've tested your code and it works pretty good. The only thing I should add to it later is to avoid non-character key strokes like 'ArrowUp' or 'ESC'. although I was hopping for some predefined method in the .NET Console library that I'm not aware of! – Shamim Dec 17 '16 at 10:37
  • @Shamim You could add a `else if(char.IsLetterOrDigit(key.KeyChar))` to catch these keys, my bad for not doing this in the first place. – Maximilian Gerhardt Dec 17 '16 at 10:39
  • It's OK, but I think I'm going with user3598756's answer for simplicity! – Shamim Dec 17 '16 at 10:53