
Protected Shared Sub WriteAt(s As String, x As Integer, y As Integer)Ĭonsole.SetCursorPosition(origCol + x, origRow + y) Protected static void WriteAt(string s, int x, int y)Ĭonsole.SetCursorPosition(origCol+x, origRow+y) This example produces the following results: Draw the top side, from right to left. Draw the right side, from bottom to top. Draw the bottom side, from left to right. Draw the left side of a 5x5 rectangle, from top to bottom. Clear the screen, then save the top and left coordinates. This example demonstrates theĬonsole::SetCursorPosition( origCol + x, origRow + y ) Note that the rectangle could be drawn with fewer steps using a combination of other strings.
The example positions the cursor, which determines where the next write will occur, to draw a 5 character by 5 character rectangle using a combination of "+", "|", and "-" strings. This example demonstrates the CursorLeft and CursorTop properties, and the SetCursorPosition and Clear methods. The example relies on a GetKeyPress method to validate the user's selection of a foreground and background color. If Array.Exists(validChars, Function(ch As Char) ch.Equals(Char.ToUpper(keypressed.Ke圜har))) Private Function GetKeyPress(msg As String, validChars() As Char) As Char ' Restore the default settings and clear the screen. If (Char.ToUpper(GetKeyPress("Display another message (Y/N): ", new Char )) = "N" Then String textToDisplay = Console.ReadLine() Save colors so they can be restored when use finishes input.ĬonsoleColor dftForeColor = Console.ForegroundColor ĬonsoleColor dftBackColor = Console.BackgroundColor ĬonsoleColor newForeColor = ConsoleColor.White ĬonsoleColor newBackColor = ConsoleColor.Black Ĭhar foreColorSelection = GetKeyPress("Select Text Color (B for Blue, R for Red, Y for Yellow): ",Ĭhar backColorSelection = GetKeyPress("Select Background Color (W for White, G for Green, M for Magenta): ",Ĭonsole.Write("Enter a message to display: ")

If the user chooses not to exit the program, the console's original foreground and background colors are restored and the Clear method is called again before re-executing the loop.

The following example uses the Clear method to clear the console before it executes a loop, prompts the user to select a foreground and background color and to enter a string to display.
