1

I would like to fill/update the entire bottom line of the console. Example:

static void Main(string[] args)
{
    Console.BufferWidth = Console.WindowWidth;
    Console.BufferHeight = Console.WindowHeight;
    Console.CursorTop = Console.WindowHeight - 1;
    string s = "";
    for(int i = 0; i < Console.BufferWidth; i++)
        s += (i%10).ToString();
    Console.Write(s);
    Console.CursorTop = 0;
    Console.ReadKey();
}

The issue is that when the text is printed, it moves to a new line. Similar questions stated to move the cursor to 0,0, however that only works when the buffer size is larger than the window size, and I would like the buffer width and the window width to be equal (to remove scroll bars). Any ideas? The closest I could get is printing to a higher line and moving it to the last line, however that will not be acceptable in the full project.

Edit: The last sentence of the question was specifically talking about movebufferarea. The reason why that won't work can be seen by this example:

static void Main(string[] args)
{
    Console.BufferWidth = Console.WindowWidth;
    Console.BufferHeight = Console.WindowHeight;
    while (!Console.KeyAvailable)
    {
        Console.CursorTop = Console.WindowHeight - 2;
        string s = "";
        for (int i = 0; i < Console.BufferWidth; i++)
            s += (i % 10).ToString();
        Console.Write(s);
        Console.MoveBufferArea(0, Console.WindowHeight - 2, Console.WindowWidth, 1, 0, Console.WindowHeight - 1);
        Thread.Sleep(10);
    }
}

The sentence will flicker every so often because it is printed first and then moved.

Qwerty01
  • 769
  • 8
  • 25
  • i = Console.CursorLeft ? – Slugart Aug 01 '14 at 16:12
  • In a sense, yes, but it's more to show each position. This is just an example since the full application is under a license not compatible with stackoverflow's contribution license. – Qwerty01 Aug 01 '14 at 18:34

3 Answers3

2

Since the cursor is always trailing after the text you've written you can either write one less character to avoid going to the next line, or simply write the characters directly into the buffer (I believe, Console.MoveBufferArea can be used for that).

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Joey
  • 344,408
  • 85
  • 689
  • 683
  • Unfortunately the entire buffer will be used in the full application, using up a line would cause unnecessary updating (which is why writing to a higher line and moving to a lower with movebufferarea won't work) – Qwerty01 Aug 01 '14 at 17:58
1

As Joey mentioned, using the MoveBufferArea method will do what you're looking to accomplish:

Console.BufferWidth = Console.WindowWidth;
Console.BufferHeight = Console.WindowHeight;

string s = "";
for (int i = 0; i < Console.BufferWidth; i++)
s += (i % 10).ToString();

Console.Write(s);

//
//  copy the buffer from its original position (0, 0) to (0, 24). MoveBufferArea
//  does NOT reposition the cursor, which will prevent the cursor from wrapping
//  to a new line when the buffer's width is filled.
Console.MoveBufferArea(0, 0, Console.BufferWidth, Console.BufferHeight, 0, 24);
Console.ReadKey();

Here's the result:

enter image description here

StoriKnow
  • 5,738
  • 6
  • 37
  • 46
  • As with Joey's response, this will cause unnecessary updating when the entire buffer is used (it will clear a line, or partial, and everything that the line used will need to be redrawn). I'm looking for an answer that wouldn't use up extra area in the console, "The closest I could get is printing to a higher line and moving it to the last line, however that will not be acceptable in the full project." Upvoted because it can solve problems for people who don't have the same requirements. – Qwerty01 Aug 01 '14 at 18:05
  • I think it may be helpful if you updated your question to include the full requirement/ what you're really trying to accomplish. – StoriKnow Aug 01 '14 at 18:22
  • I'm working on a full console wrapper which safely handles input and output simultaneously. Input will be on the last line, and the entire buffer will possibly be full, leaving no room to move and copy a sentence. Shifting the entire window down makes updates extremely inefficient, and still causes flashing due to the console printing the window twice. Edit - the other last line explained that moving parts of the buffer wouldn't work (which is what movebufferarea does) – Qwerty01 Aug 01 '14 at 18:30
0

Set the BufferHeight and BufferWidth after you've written the string.

Console.CursorTop = Console.WindowHeight - 1;
Console.SetCursorPosition(0, Console.CursorTop);
string s = "";
for (int i = 0; i < Console.BufferWidth; i++)
    s += (i % 10).ToString();
Console.Write(s);
Console.CursorTop = 0;
Console.BufferWidth = Console.WindowWidth;
Console.BufferHeight = Console.WindowHeight;
Console.ReadKey();
raj
  • 168
  • 5