C# numericUpDownのテキストの色を変更する

  • 作成日 2021.12.18
  • 更新日 2022.03.03
  • C#
C# numericUpDownのテキストの色を変更する

C#で、ForeColorを使用して、numericUpDownのテキストの色を変更するサンプルコードを記述してます。

環境

  • OS windows10 pro 64bit
  • Microsoft Visual Studio Community 2019 Version 16.7.1

ForeColor使い方

ForeColorにRGB値を指定することで、numericUpDownのテキストの色を変更することが可能です。

numericUpDown1.ForeColor = Color.FromArgb(86, 61, 124);

// 以下も可能
numericUpDown1.ForeColor = Color.Red;

サンプルコード

以下は、
「実行」ボタンをクリックすると、numericUpDownのテキストの色を変更する
サンプルコードとなります。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FormTestApp
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (numericUpDown1.ForeColor == Color.FromArgb(86, 61, 124))
            {
                //テキストの色変更
                numericUpDown1.ForeColor = Color.Red;
            }
            else
            {
                //テキストの色変更
                numericUpDown1.ForeColor = Color.FromArgb(86, 61, 124);
            }
        }
    }
}

テキストの色が変わっていることが確認できます。