Console.ForegroundColor プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Console.ForegroundColor プロパティの意味・解説 

Console.ForegroundColor プロパティ

メモ : このプロパティは、.NET Framework version 2.0新しく追加されたものです。

コンソール前景色を取得または設定します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Property ForegroundColor
 As ConsoleColor
Dim value As ConsoleColor

value = Console.ForegroundColor

Console.ForegroundColor = value
public static ConsoleColor ForegroundColor
 { get; set; }
public:
static property ConsoleColor ForegroundColor {
    ConsoleColor get ();
    void set (ConsoleColor value);
}
/** @property */
public static ConsoleColor get_ForegroundColor
 ()

/** @property */
public static void set_ForegroundColor
 (ConsoleColor value)
public static function get
 ForegroundColor () : ConsoleColor

public static function set
 ForegroundColor (value : ConsoleColor)

プロパティ
コンソール前景色、つまり、表示される文字の色指定する ConsoleColor。既定値灰色です。

例外例外
例外種類条件

ArgumentException

設定操作指定された色は、System.ConsoleColor有効なメンバではありません。

SecurityException

ユーザーに、この操作実行するためのアクセス許可がありません。

IOException

I/O エラー発生しました

解説解説
使用例使用例

Console.ForegroundColor プロパティ、Console.BackgroundColor プロパティ、および Console.ResetColor メソッド組み合わせてSystem.ConsoleColor 列挙体を使用するコード例次に示します説明する色の効果確認するには、コンソールでこの例を実行する必要があります

この例では、BlackWhite2 つ定数のみ明示的に指定されその他の定数 (BlueDarkRed など) についてはすべて間接的に指定されています。まず、GetNames メソッド使用して定数の名前を取得します。このメソッドは、Enum クラスから継承されます。次に、Enum.Parse メソッドそれぞれの名前を使用して対応する列挙型定数作成します

' This example demonstrates the ConsoleColor enumeration.
Imports System

Class Sample
   Public Shared Sub Main()
      Dim nl As [String] = Environment.NewLine
      Dim colorNames As [String]() = ConsoleColor.GetNames(GetType(ConsoleColor))
      Dim x As Integer
      
      ' ---------------------------------------------------------------------------------------
      Console.WriteLine("{0}All the foreground colors on a constant
 black background.", nl)
      Console.WriteLine("  (Black on black is not readable.){0}",
 nl)
      
      For x = 0 To colorNames.Length - 1
         Console.Write("{0,2}: ", x)
         Console.BackgroundColor = ConsoleColor.Black
         Console.ForegroundColor = CType([Enum].Parse(GetType(ConsoleColor),
 _
                                                colorNames(x)), ConsoleColor)
         Console.Write("This is foreground color {0}.",
 colorNames(x))
         Console.ResetColor()
         Console.WriteLine()
      Next x
      ' ---------------------------------------------------------------------------------------
      Console.WriteLine("{0}A constant white foreground on all
 the background colors.", nl)
      Console.WriteLine("  (White on white is not readable.){0}",
 nl)
      
      For x = 0 To colorNames.Length - 1
         Console.Write("{0,2}: ", x)
         Console.ForegroundColor = ConsoleColor.White
         Console.BackgroundColor = CType([Enum].Parse(GetType(ConsoleColor),
 _
                                                colorNames(x)), ConsoleColor)
         Console.Write("This is background color {0}.",
 colorNames(x))
         Console.ResetColor()
         Console.WriteLine()
      Next x
      ' ---------------------------------------------------------------------------------------
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'All the foreground colors on a constant black background.
'  (Black on black is not readable.)
'
' 0: This is foreground color Black.
' 1: This is foreground color DarkBlue.
' 2: This is foreground color DarkGreen.
' 3: This is foreground color DarkCyan.
' 4: This is foreground color DarkRed.
' 5: This is foreground color DarkMagenta.
' 6: This is foreground color DarkYellow.
' 7: This is foreground color Gray.
' 8: This is foreground color DarkGray.
' 9: This is foreground color Blue.
'10: This is foreground color Green.
'11: This is foreground color Cyan.
'12: This is foreground color Red.
'13: This is foreground color Magenta.
'14: This is foreground color Yellow.
'15: This is foreground color White.
'
'A constant white foreground on all the background colors.
'  (White on white is not readable.)
'
' 0: This is background color Black.
' 1: This is background color DarkBlue.
' 2: This is background color DarkGreen.
' 3: This is background color DarkCyan.
' 4: This is background color DarkRed.
' 5: This is background color DarkMagenta.
' 6: This is background color DarkYellow.
' 7: This is background color Gray.
' 8: This is background color DarkGray.
' 9: This is background color Blue.
'10: This is background color Green.
'11: This is background color Cyan.
'12: This is background color Red.
'13: This is background color Magenta.
'14: This is background color Yellow.
'15: This is background color White.
'
// This example demonstrates the ConsoleColor enumeration.
using System;

class Sample 
{
    public static void Main()
 
    {
    String nl = Environment.NewLine;
    String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));

// ---------------------------------------------------------------------------------------
    Console.WriteLine("{0}All the foreground colors on a constant black background.",
 nl);
    Console.WriteLine("  (Black on black is not readable.){0}", nl);

    for (int x = 0; x < colorNames.Length;
 x++)
    {
    Console.Write("{0,2}: ", x);
    Console.BackgroundColor = ConsoleColor.Black;
    Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);
    Console.Write("This is foreground color {0}.", colorNames[x]);
    Console.ResetColor();
    Console.WriteLine();
    }
// ---------------------------------------------------------------------------------------
    Console.WriteLine("{0}A constant white foreground on all the background
 colors.", nl);
    Console.WriteLine("  (White on white is not readable.){0}", nl);

    for (int x = 0; x < colorNames.Length;
 x++)
    {
    Console.Write("{0,2}: ", x);
    Console.ForegroundColor = ConsoleColor.White;
    Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);
    Console.Write("This is background color {0}.", colorNames[x]);
    Console.ResetColor();
    Console.WriteLine();
    }
// ---------------------------------------------------------------------------------------
    }
}
/*
This example produces the following results:

All the foreground colors on a constant black background.
  (Black on black is not readable.)

 0: This is foreground color Black.
 1: This is foreground color DarkBlue.
 2: This is foreground color DarkGreen.
 3: This is foreground color DarkCyan.
 4: This is foreground color DarkRed.
 5: This is foreground color DarkMagenta.
 6: This is foreground color DarkYellow.
 7: This is foreground color Gray.
 8: This is foreground color DarkGray.
 9: This is foreground color Blue.
10: This is foreground color Green.
11: This is foreground color Cyan.
12: This is foreground color Red.
13: This is foreground color Magenta.
14: This is foreground color Yellow.
15: This is foreground color White.

A constant white foreground on all the background colors.
  (White on white is not readable.)

 0: This is background color Black.
 1: This is background color DarkBlue.
 2: This is background color DarkGreen.
 3: This is background color DarkCyan.
 4: This is background color DarkRed.
 5: This is background color DarkMagenta.
 6: This is background color DarkYellow.
 7: This is background color Gray.
 8: This is background color DarkGray.
 9: This is background color Blue.
10: This is background color Green.
11: This is background color Cyan.
12: This is background color Red.
13: This is background color Magenta.
14: This is background color Yellow.
15: This is background color White.
*/
// This example demonstrates the ConsoleColor enumeration.
using namespace System;
int main()
{
   String^ nl = Environment::NewLine;
   array<String^>^colorNames = Enum::GetNames( ConsoleColor::typeid );

   // ---------------------------------------------------------------------------------------
   Console::WriteLine( "{0}All the foreground colors on a constant black background.",
 nl );
   Console::WriteLine( "  (Black on black is not readable.){0}", nl );
   for ( int x = 0; x < colorNames->Length;
 x++ )
   {
      Console::Write( "{0,2}: ", x );
      Console::BackgroundColor = ConsoleColor::Black;
      Console::ForegroundColor =  *dynamic_cast<ConsoleColor^>(Enum::Parse(
 ConsoleColor::typeid, colorNames[ x ] ));
      Console::Write( "This is foreground color {0}.", colorNames[ x ]
 );
      Console::ResetColor();
      Console::WriteLine();
   }
   Console::WriteLine( "{0}A constant white foreground on all the background
 colors.", nl );
   Console::WriteLine( "  (White on white is not readable.){0}", nl );
   for ( int x = 0; x < colorNames->Length;
 x++ )
   {
      Console::Write( "{0,2}: ", x );
      Console::ForegroundColor = ConsoleColor::White;
      Console::BackgroundColor =  *dynamic_cast<ConsoleColor^>(Enum::Parse(
 ConsoleColor::typeid, colorNames[ x ] ));
      Console::Write( "This is background color {0}.", colorNames[ x ]
 );
      Console::ResetColor();
      Console::WriteLine();

   }
}

/*
This example produces the following results:

All the foreground colors on a constant black background.
(Black on black is not readable.)

0: This is foreground color Black.
1: This is foreground color DarkBlue.
2: This is foreground color DarkGreen.
3: This is foreground color DarkCyan.
4: This is foreground color DarkRed.
5: This is foreground color DarkMagenta.
6: This is foreground color DarkYellow.
7: This is foreground color Gray.
8: This is foreground color DarkGray.
9: This is foreground color Blue.
10: This is foreground color Green.
11: This is foreground color Cyan.
12: This is foreground color Red.
13: This is foreground color Magenta.
14: This is foreground color Yellow.
15: This is foreground color White.

A constant white foreground on all the background colors.
(White on white is not readable.)

0: This is background color Black.
1: This is background color DarkBlue.
2: This is background color DarkGreen.
3: This is background color DarkCyan.
4: This is background color DarkRed.
5: This is background color DarkMagenta.
6: This is background color DarkYellow.
7: This is background color Gray.
8: This is background color DarkGray.
9: This is background color Blue.
10: This is background color Green.
11: This is background color Cyan.
12: This is background color Red.
13: This is background color Magenta.
14: This is background color Yellow.
15: This is background color White.
*/
// This example demonstrates the ConsoleColor enumeration.
import System.*;

class Sample
{
    public static void main(String[]
 args)
    {
        String nl = Environment.get_NewLine();
        String colorNames[] = ConsoleColor.GetNames(ConsoleColor.class.ToType());
        // ----------------------------------------------------------------------
        Console.WriteLine("{0}All the foreground colors on a constant black
 "
            + " background.", nl);
        Console.WriteLine("  (Black on black is not readable.){0}", nl);

        for (int x = 0; x < colorNames.get_Length();
 x++) {
            Console.Write("{0,2}: ", System.Convert.ToString(x));
            Console.set_BackgroundColor(ConsoleColor.Black);
            Console.set_ForegroundColor(((ConsoleColor)(Enum.Parse(
                ConsoleColor.class.ToType(), System.Convert.ToString(
                colorNames.get_Item(x))))));
            Console.Write("This is foreground color {0}.",
                colorNames.get_Item(x));
            Console.ResetColor();
            Console.WriteLine();
        }
        // ----------------------------------------------------------------------
        Console.WriteLine("{0}A constant white foreground on all the "
            + "background colors.", nl);
        Console.WriteLine("  (White on white is not readable.){0}", nl);

        for (int x = 0; x < colorNames.get_Length();
 x++) {
            Console.Write("{0,2}: ", System.Convert.ToString(x));
            Console.set_ForegroundColor(ConsoleColor.White);
            Console.set_BackgroundColor(((ConsoleColor)(Enum.Parse(
                ConsoleColor.class.ToType(), 
                System.Convert.ToString(colorNames.get_Item(x))))));
            Console.Write("This is background color {0}.", 
                colorNames.get_Item(x));
            Console.ResetColor();
            Console.WriteLine();
        }
        // ----------------------------------------------------------------------
    } //main
} //Sample
/*
This example produces the following results:

All the foreground colors on a constant black background.
  (Black on black is not readable.)

 0: This is foreground color Black.
 1: This is foreground color DarkBlue.
 2: This is foreground color DarkGreen.
 3: This is foreground color DarkCyan.
 4: This is foreground color DarkRed.
 5: This is foreground color DarkMagenta.
 6: This is foreground color DarkYellow.
 7: This is foreground color Gray.
 8: This is foreground color DarkGray.
 9: This is foreground color Blue.
10: This is foreground color Green.
11: This is foreground color Cyan.
12: This is foreground color Red.
13: This is foreground color Magenta.
14: This is foreground color Yellow.
15: This is foreground color White.

A constant white foreground on all the background colors.
  (White on white is not readable.)

 0: This is background color Black.
 1: This is background color DarkBlue.
 2: This is background color DarkGreen.
 3: This is background color DarkCyan.
 4: This is background color DarkRed.
 5: This is background color DarkMagenta.
 6: This is background color DarkYellow.
 7: This is background color Gray.
 8: This is background color DarkGray.
 9: This is background color Blue.
10: This is background color Green.
11: This is background color Cyan.
12: This is background color Red.
13: This is background color Magenta.
14: This is background color Yellow.
15: This is background color White.
*/
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からConsole.ForegroundColor プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からConsole.ForegroundColor プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からConsole.ForegroundColor プロパティ を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Console.ForegroundColor プロパティ」の関連用語

Console.ForegroundColor プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Console.ForegroundColor プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS