Console.Clear メソッド
アセンブリ: mscorlib (mscorlib.dll 内)
構文
例外
解説
Clear メソッドを実行することは、コマンド プロンプト ウィンドウから、MS-DOS の cls コマンドを呼び出すことに相当します。Clear メソッドを呼び出すと、カーソルが自動的にウィンドウの左上隅にスクロールされ、また、現在の前景色と背景色を使って画面バッファの内容が空に設定されます。
CursorLeft プロパティと CursorTop プロパティ、および SetCursorPosition メソッドと Clear メソッドの使用例を次に示します。この例では、次のテキストの書き込み位置となるカーソルを適宜配置することによって、"+"、"|"、"-" の文字列を使いながら 5 文字 × 5 文字の四角形を描画しています。他の文字列を組み合わせれば、より少ないステップ数で四角形を描画できます。
' This example demonstrates the ' Console.CursorLeft and ' Console.CursorTop properties, and the ' Console.SetCursorPosition and ' Console.Clear methods. Imports System Class Sample Protected Shared origRow As Integer Protected Shared origCol As Integer Protected Shared Sub WriteAt(s As String, x As Integer, y As Integer) Try Console.SetCursorPosition(origCol + x, origRow + y) Console.Write(s) Catch e As ArgumentOutOfRangeException Console.Clear() Console.WriteLine(e.Message) End Try End Sub 'WriteAt Public Shared Sub Main() ' Clear the screen, then save the top and left coordinates. Console.Clear() origRow = Console.CursorTop origCol = Console.CursorLeft ' Draw the left side of a 5x5 rectangle, from top to bottom. WriteAt("+", 0, 0) WriteAt("|", 0, 1) WriteAt("|", 0, 2) WriteAt("|", 0, 3) WriteAt("+", 0, 4) ' Draw the bottom side, from left to right. WriteAt("-", 1, 4) ' shortcut: WriteAt("---", 1, 4) WriteAt("-", 2, 4) ' ... WriteAt("-", 3, 4) ' ... WriteAt("+", 4, 4) ' Draw the right side, from bottom to top. WriteAt("|", 4, 3) WriteAt("|", 4, 2) WriteAt("|", 4, 1) WriteAt("+", 4, 0) ' Draw the top side, from right to left. WriteAt("-", 3, 0) ' shortcut: WriteAt("---", 1, 0) WriteAt("-", 2, 0) ' ... WriteAt("-", 1, 0) ' ... ' WriteAt("All done!", 0, 6) Console.WriteLine() End Sub 'Main End Class 'Sample ' 'This example produces the following results: ' '+---+ '| | '| | '| | '+---+ ' 'All done! '
// This example demonstrates the // Console.CursorLeft and // Console.CursorTop properties, and the // Console.SetCursorPosition and // Console.Clear methods. using System; class Sample { protected static int origRow; protected static int origCol; protected static void WriteAt(string s, int x, int y) { try { Console.SetCursorPosition(origCol+x, origRow+y); Console.Write(s); } catch (ArgumentOutOfRangeException e) { Console.Clear(); Console.WriteLine(e.Message); } } public static void Main() { // Clear the screen, then save the top and left coordinates. Console.Clear(); origRow = Console.CursorTop; origCol = Console.CursorLeft; // Draw the left side of a 5x5 rectangle, from top to bottom. WriteAt("+", 0, 0); WriteAt("|", 0, 1); WriteAt("|", 0, 2); WriteAt("|", 0, 3); WriteAt("+", 0, 4); // Draw the bottom side, from left to right. WriteAt("-", 1, 4); // shortcut: WriteAt("---", 1, 4) WriteAt("-", 2, 4); // ... WriteAt("-", 3, 4); // ... WriteAt("+", 4, 4); // Draw the right side, from bottom to top. WriteAt("|", 4, 3); WriteAt("|", 4, 2); WriteAt("|", 4, 1); WriteAt("+", 4, 0); // Draw the top side, from right to left. WriteAt("-", 3, 0); // shortcut: WriteAt("---", 1, 0) WriteAt("-", 2, 0); // ... WriteAt("-", 1, 0); // ... // WriteAt("All done!", 0, 6); Console.WriteLine(); } } /* This example produces the following results: +---+ | | | | | | +---+ All done! */
// This example demonstrates the // Console.CursorLeft and // Console.CursorTop properties, and the // Console.SetCursorPosition and // Console.Clear methods. using namespace System; int origRow; int origCol; void WriteAt( String^ s, int x, int y ) { try { Console::SetCursorPosition( origCol + x, origRow + y ); Console::Write( s ); } catch ( ArgumentOutOfRangeException^ e ) { Console::Clear(); Console::WriteLine( e->Message ); } } int main() { // Clear the screen, then save the top and left coordinates. Console::Clear(); origRow = Console::CursorTop; origCol = Console::CursorLeft; // Draw the left side of a 5x5 rectangle, from top to bottom. WriteAt( "+", 0, 0 ); WriteAt( "|", 0, 1 ); WriteAt( "|", 0, 2 ); WriteAt( "|", 0, 3 ); WriteAt( "+", 0, 4 ); // Draw the bottom side, from left to right. WriteAt( "-", 1, 4 ); // shortcut: WriteAt("---", 1, 4) WriteAt( "-", 2, 4 ); // ... WriteAt( "-", 3, 4 ); // ... WriteAt( "+", 4, 4 ); // Draw the right side, from bottom to top. WriteAt( "|", 4, 3 ); WriteAt( "|", 4, 2 ); WriteAt( "|", 4, 1 ); WriteAt( "+", 4, 0 ); // Draw the top side, from right to left. WriteAt( "-", 3, 0 ); // shortcut: WriteAt("---", 1, 0) WriteAt( "-", 2, 0 ); // ... WriteAt( "-", 1, 0 ); // ... // WriteAt( "All done!", 0, 6 ); Console::WriteLine(); } /* This example produces the following results: +---+ | | | | | | +---+ All done! */
// This example demonstrates the // Console.CursorLeft and // Console.CursorTop properties, and the // Console.SetCursorPosition and // Console.Clear methods. import System.*; class Sample { protected static int origRow; protected static int origCol; protected static void WriteAt(String s, int x, int y) { try { Console.SetCursorPosition(origCol + x, origRow + y); Console.Write(s); } catch (ArgumentOutOfRangeException e) { Console.Clear(); Console.WriteLine(e.get_Message()); } } //WriteAt public static void main(String[] args) { // Clear the screen, then save the top and left coordinates. Console.Clear(); origRow = Console.get_CursorTop(); origCol = Console.get_CursorLeft(); // Draw the left side of a 5x5 rectangle, from top to bottom. WriteAt("+", 0, 0); WriteAt("|", 0, 1); WriteAt("|", 0, 2); WriteAt("|", 0, 3); WriteAt("+", 0, 4); // Draw the bottom side, from left to right. WriteAt("-", 1, 4); // shortcut: WriteAt("---", 1, 4) WriteAt("-", 2, 4); // ... WriteAt("-", 3, 4); // ... WriteAt("+", 4, 4); // Draw the right side, from bottom to top. WriteAt("|", 4, 3); WriteAt("|", 4, 2); WriteAt("|", 4, 1); WriteAt("+", 4, 0); // Draw the top side, from right to left. WriteAt("-", 3, 0); // shortcut: WriteAt("---", 1, 0) WriteAt("-", 2, 0); // ... WriteAt("-", 1, 0); // ... // WriteAt("All done!", 0, 6); Console.WriteLine(); } //main } //Sample /* This example produces the following results: +---+ | | | | | | +---+ All done! */
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Weblioに収録されているすべての辞書からConsole.Clear メソッドを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からConsole.Clear メソッドを検索
- Console.Clear メソッドのページへのリンク