本文整理汇总了C#中System.Console.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# Console.Clear方法的具体用法?C# Console.Clear怎么用?C# Console.Clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Console
的用法示例。
在下文中一共展示了Console.Clear方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
// Save colors so they can be restored when use finishes input.
ConsoleColor dftForeColor = Console.ForegroundColor;
ConsoleColor dftBackColor = Console.BackgroundColor;
bool continueFlag = true;
Console.Clear();
do {
ConsoleColor newForeColor = ConsoleColor.White;
ConsoleColor newBackColor = ConsoleColor.Black;
Char foreColorSelection = GetKeyPress("Select Text Color (B for Blue, R for Red, Y for Yellow): ",
new Char[] { 'B', 'R', 'Y' } );
switch (foreColorSelection) {
case 'B':
case 'b':
newForeColor = ConsoleColor.DarkBlue;
break;
case 'R':
case 'r':
newForeColor = ConsoleColor.DarkRed;
break;
case 'Y':
case 'y':
newForeColor = ConsoleColor.DarkYellow;
break;
}
Char backColorSelection = GetKeyPress("Select Background Color (W for White, G for Green, M for Magenta): ",
new Char[] { 'W', 'G', 'M' });
switch (backColorSelection) {
case 'W':
case 'w':
newBackColor = ConsoleColor.White;
break;
case 'G':
case 'g':
newBackColor = ConsoleColor.Green;
break;
case 'M':
case 'm':
newBackColor = ConsoleColor.Magenta;
break;
}
Console.WriteLine();
Console.Write("Enter a message to display: ");
String textToDisplay = Console.ReadLine();
Console.WriteLine();
Console.ForegroundColor = newForeColor;
Console.BackgroundColor = newBackColor;
Console.WriteLine(textToDisplay);
Console.WriteLine();
if (Char.ToUpper(GetKeyPress("Display another message (Y/N): ", new Char[] { 'Y', 'N' } )) == 'N')
continueFlag = false;
// Restore the default settings and clear the screen.
Console.ForegroundColor = dftForeColor;
Console.BackgroundColor = dftBackColor;
Console.Clear();
} while (continueFlag);
}
private static Char GetKeyPress(String msg, Char[] validChars)
{
ConsoleKeyInfo keyPressed;
bool valid = false;
Console.WriteLine();
do {
Console.Write(msg);
keyPressed = Console.ReadKey();
Console.WriteLine();
if (Array.Exists(validChars, ch => ch.Equals(Char.ToUpper(keyPressed.KeyChar))))
valid = true;
} while (! valid);
return keyPressed.KeyChar;
}
}
示例2: WriteAt
// 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();
}
}
输出:
+---+ | | | | | | +---+ All done!
示例3: Console.Clear()
//引入命名空间
using System;
public class Test{
static void Main(string[] args){
// Change the Console appearance and redisplay.
Console.Title = "Cleared / Colored Console";
Console.ForegroundColor = ConsoleColor.Blue;
Console.BackgroundColor = ConsoleColor.Yellow;
Console.Clear();
Console.WriteLine("Press Enter to change the Console's appearance.");
Console.ReadLine();
}
}