Tadeusz
Tadeusz

Reputation: 6913

How to write in current line of Console?

I am writing a console application and I need to know, how to write in current line with shift of lines. I try to explain this on the next example: Let It console lines with their numbers and contents along with cursor position.

  1. Hello!
  2. This is my command shell.
  3. Please write something: _

When I call my method for writing in console text "lalala", i want to see that:

  1. Hello!
  2. This is my command shell.
  3. lalala
  4. Please write something: _

If I use Console.WriteLine method I see the next:

  1. Hello!
  2. This is my command shell.
  3. Please write something: lalala
  4. _

Please, help me to realise this feature.

Upvotes: 1

Views: 3467

Answers (7)

Jonathan Camilleri
Jonathan Camilleri

Reputation: 631

I realize that this is an old question, however I have been searching this stuff and here is how it can be coded:

Console.WriteLine("Hello!");
Console.WriteLine("This is my command shell.");
string text = "";
string toWrite = "Please write something: ";
while (text != "quit")
{
    Console.Write(toWrite);
    text = Console.ReadLine();
    Console.SetCursorPosition(0, Console.CursorTop - 1);
    Console.WriteLine(text.PadRight(text.Length + toWrite.Length));
}

The Console.SetCursorPosition puts the cursor back to the line that was written into, then overwrite what is written with the text and a padding equivalent to how many chars the text had.

Upvotes: 0

Liam McInroy
Liam McInroy

Reputation: 4366

try something like this

Console.Write("Hello\nThis is My Command Shell\nlalala\nPlease Enter Something:___");

if course that would end up having them all appear at the same time, but if your good with that this will work

Will look like this

console application

Upvotes: 1

Rajesh
Rajesh

Reputation: 7886

Please find the code for the above scenario:

private static void ReadAndWriteToConsole()
    {
        var mystrings = new List<string>();

        mystrings.Add("Hello!");
        mystrings.Add("This is my command shell.");

        var input = WriteToConsole(mystrings);
        while (input.ToLower() != "exit")
        {
            mystrings.Add(input);
            Console.Clear();
            input = WriteToConsole(mystrings);
        }
    }

    private static string WriteToConsole(IEnumerable<string> variables )
    {
        foreach (var str in variables)
        {
            Console.WriteLine(str);
        }
        Console.Write("Please write something:");
        return Console.ReadLine();
    }

Hope that helps.

NOTE: If you want the number of each string then use a for loop instead of foreach and just print the variable used in the console.writeline.

Upvotes: 1

gabsferreira
gabsferreira

Reputation: 3147

Console.WriteLine("1.Hello!");
Console.WriteLine("2.This is my command shell.");
Console.WriteLine("3.lalala");
Console.Write("4.Please write something:");
Console.Read();

Upvotes: 2

Bali C
Bali C

Reputation: 31251

If I understand your question right I think you need to use

Console.Write("text"); 

This will write on the same line as the cursor is currently on.

Rather than:

Console.WriteLine("text");

This will create a new line in the console each time it is called.

Upvotes: 1

Prafulla
Prafulla

Reputation: 1949

Console.SetCursorPosition is the poison you are look for. More details on http://msdn.microsoft.com/en-us/library/system.console.setcursorposition.aspx

Upvotes: 4

Daniel
Daniel

Reputation: 157

As you didn't provide any code i assume you're using Console.WriteLine("Please write something"); to print out text. Since this will add an \n to the text you want to print you should rather use Console.Write("Please write something") then do an Console.ReadLine(); to get the input and handle the \n by yourself.

Upvotes: 3

Related Questions