「Enabled = false; した後に文字色変えたらいいじゃんって思ったけど出来ませんでしたごめんなさい」のコード


Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testTextBoXColor
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (textBox1.ReadOnly)
				textBox1.ReadOnly = false;
			else
				textBox1.ReadOnly = true;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (textBox1.Enabled)
				textBox1.Enabled = false;
			else
				textBox1.Enabled = true;
		}

		private void button3_Click(object sender, EventArgs e)
		{
			if (textBox1.Enabled)
			{
				textBox1.Enabled = false;
				// http://dobon.net/vb/dotnet/control/tbenabledfalse.html
				// ただし、ForeColorはこの方法では変更できないようです。(.NET Framework 1.1、2.0で確認)
				textBox1.ForeColor = Color.Black;
			}
			else
			{
				textBox1.Enabled = true;
			}
		}
	}
}

Form1.Designer.cs

namespace testTextBoXColor
{
	partial class Form1
	{
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// 使用中のリソースをすべてクリーンアップします。
		/// </summary>
		/// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows フォーム デザイナで生成されたコード

		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.ForeColor = System.Drawing.Color.Black;
			this.textBox1.Location = new System.Drawing.Point(22, 25);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(215, 154);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "\r\nのようにして、最後に付け加えた\r\nend の文字だけ色をかえているのですが、\r\n選択された状態になってしまいます。\r\nマウスでドラックしている状態です。\r\n\r" +
				"\nこれを解除した状態で文字色をかえるにはどうしたらいいのでしょうか。\r\n質問投稿日時:08/07/02 12:43\r\n質問番号:4145471\r\nこの質問に対す" +
				"る回答は締め切られました。";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(22, 198);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(75, 63);
			this.button1.TabIndex = 1;
			this.button1.Text = "ReadOnly on/off";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(103, 198);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(75, 63);
			this.button2.TabIndex = 1;
			this.button2.Text = "Enabled on/off";
			this.button2.UseVisualStyleBackColor = true;
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(184, 198);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(75, 63);
			this.button3.TabIndex = 1;
			this.button3.Text = "ぼくの考えた対策 on/off";
			this.button3.UseVisualStyleBackColor = true;
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(292, 275);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#endregion

		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
	}
}