12

I would like to set the position of the cursor in the Console to the last visible line. How can I do this?

Cheers,

Pete

  • 1
    Define 'last visible line.' Do you mean the last line of text (cursor should be their automatically) or at the bottom of the visible window regardless of what text is in existance? – AllenG Aug 04 '10 at 16:19
  • At the bottom of the visible window regardless of what text is already there. –  Aug 04 '10 at 21:53

2 Answers2

25

If you mean the last line of the window, you can use a mixture of Console.CursorTop, and Console.WindowHeight and Console.WindowTop. Sample code:

using System;

class Test
{
    static void Main()
    {
        Console.Write("Hello");
        WriteOnBottomLine("Bottom!");
        Console.WriteLine(" there");
    }

    static void WriteOnBottomLine(string text)
    {
        int x = Console.CursorLeft;
        int y = Console.CursorTop;
        Console.CursorTop = Console.WindowTop + Console.WindowHeight - 1;
        Console.Write(text);
        // Restore previous position
        Console.SetCursorPosition(x, y);
    }
}

Note that this has to take account of Console.WindowTop to find out where you are within the buffer...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Thank you, I was gonna just write this, but I'm glad I googled it. Can I suggested editing your function to move the cursor to left = 0? I'm assuming anyone who needs to write to the bottom of the screen probably also wants to write to the left of the screen. – Sidney Sep 11 '15 at 19:57
  • 1
    @Sidney: Do you mean `Console.CursorLeft = Console.WindowLeft`? (I don't have time to test this in various configurations at the moment.) – Jon Skeet Sep 12 '15 at 12:07
1

I also had to solve this problem and came out with this:

public class Program
{
  static int id = 0 , idOld = 0, idSelected = -1;
  static string[] samples;

  public static void Main()
  {
    Console.BackgroundColor = ConsoleColor.DarkBlue;
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WindowWidth = 90;
    Console.WindowHeight = 36;
    Console.WindowTop = 5;
    Console.Title = "My Samples Application";
    Console.InputEncoding = Encoding.GetEncoding("windows-1251");

    samples = new string[50];
    for (int i = 0; i < samples.Length; i++)
      samples[i] = "Sample" + i;
    LoopSamples();
  }

  static void SelectRow(int y, bool select)
  {
    Console.CursorTop = y + 1;
    Console.ForegroundColor = select ? ConsoleColor.Red : ConsoleColor.Yellow;
    Console.WriteLine("\t{0}", samples[y]);
    Console.CursorTop = y;
  }

  static void LoopSamples()
  {
    int last = samples.Length - 1;
    ShowSamples();
    SelectRow(0, true);
    while (true)
    {
      while (idSelected == -1)
      {
        idOld = id;
        ConsoleKey key = Console.ReadKey(true).Key;
        switch (key)
        {
          case ConsoleKey.UpArrow:
          case ConsoleKey.LeftArrow: if (--id < 0) id = last; break;
          case ConsoleKey.DownArrow:
          case ConsoleKey.RightArrow: if (++id > last) id = 0; break;
          case ConsoleKey.Enter: idSelected = id; return;
          case ConsoleKey.Escape: return;
        }
        SelectRow(idOld, false);
        SelectRow(id, true);
      }
    }
  }

  static void ShowSamples()
  {
    Console.Clear();
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WriteLine("Use arrow keys to select a sample. Then press 'Enter'. Esc - to Quit");
    for (int i = 0; i < samples.Length; i++)
      Console.WriteLine("\t{0}", samples[i]);
  }
}