Console.SetCursorPosition(Int32, Int32) Methode

Definition

Legt die Position des Cursors fest.Sets the position of the cursor.

public:
 static void SetCursorPosition(int left, int top);
public static void SetCursorPosition (int left, int top);
static member SetCursorPosition : int * int -> unit
Public Shared Sub SetCursorPosition (left As Integer, top As Integer)

Parameter

left
Int32

Die Spaltenposition des Cursors.The column position of the cursor. Spalten werden von links nach rechts beginnend mit 0 nummeriert.Columns are numbered from left to right starting at 0.

top
Int32

Die Zeilenposition des Cursors.The row position of the cursor. Zeilen werden von oben nach unten nummeriert und beginnen mit 0.Rows are numbered from top to bottom starting at 0.

Ausnahmen

left oder top ist kleiner als 0.left or top is less than zero.

Oder-or- left ist größer gleich BufferWidth.left is greater than or equal to BufferWidth.

Oder-or- top ist größer gleich BufferHeight.top is greater than or equal to BufferHeight.

Der Benutzer besitzt keine Berechtigung zum Ausführen dieser Aktion.The user does not have permission to perform this action.

E/A-FehlerAn I/O error occurred.

Beispiele

In diesem Beispiel werden die Eigenschaften CursorLeft und CursorTop sowie die Methoden SetCursorPosition und Clear veranschaulicht.This example demonstrates the CursorLeft and CursorTop properties, and the SetCursorPosition and Clear methods. Im Beispiel wird der Cursor positioniert, der bestimmt, wo der nächste Schreibvorgang ausgeführt wird, um mit einer Kombination aus den Zeichen folgen "+", "|" und "-" ein 5 Zeichen um 5 Zeichen zu zeichnen.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. Beachten Sie, dass das Rechteck mit weniger Schritten gezeichnet werden kann, indem eine Kombination aus anderen Zeichen folgen verwendet wird.Note that the rectangle could be drawn with fewer steps using a combination of other strings.

// 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.
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.
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
   
   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
End Class
'
'This example produces the following results:
'
'+---+
'|   |
'|   |
'|   |
'+---+
'
'All done!
'

Hinweise

Verwenden Sie die SetCursorPosition-Methode, um anzugeben, wo der nächste Schreibvorgang im Konsolenfenster beginnen soll.Use the SetCursorPosition method to specify where the next write operation in the console window is to begin. Wenn die angegebene Cursorposition außerhalb des Bereichs liegt, der derzeit im Konsolenfenster sichtbar ist, wird der Fenster Ursprung automatisch geändert, um den Cursor sichtbar zu machen.If the specified cursor position is outside the area that is currently visible in the console window, the window origin changes automatically to make the cursor visible.

Der Cursor wechselt automatisch zur nächsten Zeichenposition, wenn ein Zeichen in das Konsolenfenster geschrieben wird.The cursor automatically moves to the next character position each time a character is written to the console window. Wenn sich der Cursor in der unteren rechten Zeichenposition des Konsolenfensters befindet, bewirkt der nächste Schreibvorgang, dass das Konsolenfenster einen Bildlauf durchführen kann, sodass der Cursor sichtbar bleibt.If the cursor is at the bottom right character position of the console window, the next write operation causes the console window to scroll so the cursor remains visible. Wenn Sie ein Zeichen an die untere rechte Zeichenposition schreiben möchten, ohne dass im Konsolenfenster ein Bildlauf durchgeführt werden soll, verwenden Sie die MoveBufferArea-Methode, um ein Zeichen an diese Position zu verschieben.If you want to write a character to the bottom right character position without causing the console window to scroll, use the MoveBufferArea method to move a character to that position.

Gilt für: