ใช้เมธอด Console.Clear() เพื่อล้างหน้าจอและบัฟเฟอร์คอนโซล เมื่อเรียกวิธีการล้าง เคอร์เซอร์จะเลื่อนไปที่มุมซ้ายบนของหน้าต่างโดยอัตโนมัติ
ที่นี่ เราได้ล้างหน้าจอแล้วตั้งค่า ForegroundColor และ BackgroundColor -
ConsoleColor newForeColor = ConsoleColor.Blue; ConsoleColor newBackColor = ConsoleColor.Yellow;
ต่อไปนี้เป็นรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
using System.Collections.Generic;
class Program {
static void Main() {
ConsoleColor foreColor = Console.ForegroundColor;
ConsoleColor backColor = Console.BackgroundColor;
Console.WriteLine("Clearing the screen!");
Console.Clear();
ConsoleColor newForeColor = ConsoleColor.Blue;
ConsoleColor newBackColor = ConsoleColor.Yellow;
}
} ผลลัพธ์
Clearing the screen!