Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Console.MoveBufferArea() Method in C#
The Console.MoveBufferArea() method in C# is used to copy a specified source area of the screen buffer to a specified destination area. This method allows you to move text and formatting from one part of the console window to another without rewriting the content.
Syntax
Following is the syntax for the Console.MoveBufferArea() method −
public static void MoveBufferArea(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight, int targetLeft, int targetTop);
Parameters
- sourceLeft − The leftmost column of the source area (0-based index).
- sourceTop − The topmost row of the source area (0-based index).
- sourceWidth − The number of columns in the source area.
- sourceHeight − The number of rows in the source area.
- targetLeft − The leftmost column of the destination area.
- targetTop − The topmost row of the destination area.
Using MoveBufferArea() to Relocate Text
Example
using System;
class Demo {
public static void Main(string[] args) {
Console.Clear();
Console.WriteLine("Original text at position (0,0)");
Console.WriteLine("This will be moved!");
// Wait briefly to show original position
System.Threading.Thread.Sleep(1000);
// Move the buffer area containing the text
Console.MoveBufferArea(0, 0, 30, 2, 10, 5);
Console.SetCursorPosition(0, 8);
Console.WriteLine("Text has been moved to position (10,5)");
}
}
The output shows the text relocated to the new position −
Text appears first at top-left, then moves to position (10,5) "Text has been moved to position (10,5)" appears at bottom
Using MoveBufferArea() with Custom Fill Character
There is an overloaded version that allows specifying a fill character for the source area −
public static void MoveBufferArea(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
int targetLeft, int targetTop, char sourceChar, ConsoleColor sourceForeColor,
ConsoleColor sourceBackColor);
Example
using System;
class Demo {
public static void Main(string[] args) {
Console.Clear();
Console.WriteLine("HELLO WORLD");
Console.WriteLine("This text will be moved and source filled with dashes");
// Move buffer area and fill source with dashes
Console.MoveBufferArea(0, 0, 50, 2, 15, 8, '-', ConsoleColor.White, ConsoleColor.Black);
Console.SetCursorPosition(0, 12);
Console.WriteLine("Source area filled with dashes, content moved to (15,8)");
}
}
The output of the above code is −
Source area shows dashes (----) where original text was Original text appears at new position (15,8) Message appears at bottom explaining the operation
Practical Use Cases
- Creating scrolling effects in console applications
- Moving menus or dialog boxes to different screen positions
- Implementing console-based animations by moving text blocks
- Reorganizing console layout dynamically during program execution
Conclusion
The Console.MoveBufferArea() method provides an efficient way to relocate portions of console content without rewriting text. It's particularly useful for creating dynamic console interfaces, animations, and reorganizing screen layouts in console-based applications.
