Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C#에서 콘솔 CursorLeft 위치를 변경하는 방법

C#에서 콘솔 화면의 커서 가로 위치를 변경하려면 Console.CursorLeft 속성을 사용하면 됩니다. 이 속성은 콘솔 버퍼 내에서 커서가 위치할 열(column) 번호를 설정하며, 텍스트를 원하는 지점에 출력할 때 유용하게 활용됩니다.

사용 예제

다음 예제는 콘솔의 배경색과 전경색을 변경한 후, CursorLeft 속성으로 커서 위치를 조정하는 과정을 보여줍니다.

using System;
class Demo {
   public static void Main (string[] args) {
      Console.BackgroundColor = ConsoleColor.Blue;
      Console.WriteLine("Background color changed = "+Console.BackgroundColor);
      Console.ForegroundColor = ConsoleColor.Yellow;
      Console.WriteLine("\nForeground color changed = "+Console.ForegroundColor);
      Console.CursorLeft = 30;
      Console.Write("CursorLeft position: "+Console.CursorLeft);
   }
}

코드 설명

  • Console.BackgroundColor = ConsoleColor.Blue; — 콘솔의 배경색을 파란색으로 설정합니다.
  • Console.ForegroundColor = ConsoleColor.Yellow; — 글자 색상을 노란색으로 변경합니다.
  • Console.CursorLeft = 30; — 커서를 현재 줄의 30번째 열로 이동시킵니다.
  • 이후 Console.Write()를 호출하면 지정된 위치부터 텍스트가 출력됩니다.

실행 결과

위 코드를 실행하면 다음과 같은 출력 결과를 확인할 수 있습니다.

C#에서 콘솔 CursorLeft 위치를 변경하는 방법