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

  • 作成日 2021.09.06
  • 更新日 2022.04.11
  • C#
C# maskedTextBoxのテキストの色を変更する

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

環境

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

maskedTextBox使い方

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

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

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

サンプルコード

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

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))
            {
                //テキストの色変更
                maskedTextBox1.ForeColor = Color.Red;
            }
            else
            {
                //テキストの色変更
                maskedTextBox1.ForeColor = Color.FromArgb(86, 61, 124);
            }
        }
    }
}

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