C# カスタムコントロール
あるようでない、テキストの中身がないとき、灰色でヘルプを表示するTextBoxコントロール。文字が入ると消えます。
namespace MyLib.Controls { public class TextBoxEx : System.Windows.Forms.TextBox { public TextBoxEx() :base() { SetUp(); } private void SetUp() { helpLabel = new System.Windows.Forms.Label(); helpLabel.AutoSize = true; helpLabel.ForeColor = System.Drawing.Color.Gray; this.Controls.Add(helpLabel); this.SizeChanged += new EventHandler(TextBoxEx_SizeChanged); helpLabel.Click += new EventHandler(helpLabel_Click); this.TextChanged += new EventHandler(TextBoxEx_TextChanged); helpLabel.Visible = true; } void TextBoxEx_TextChanged(object sender, EventArgs e) { if (this.TextLength == 0) { helpLabel.Visible = true; } else { helpLabel.Visible = false; } } void helpLabel_Click(object sender, EventArgs e) { this.Focus(); } void TextBoxEx_SizeChanged(object sender, EventArgs e) { helpLabel.Left = this.ClientRectangle.Width / 2 - helpLabel.Width / 2; helpLabel.Top = this.ClientRectangle.Height / 2 - helpLabel.Height / 2; } public string HelpText { get { return helpLabel.Text; } set { helpLabel.Text = value; OnSizeChanged(new EventArgs()); } } public System.Drawing.Font HelpFont { get { return helpLabel.Font; } set { helpLabel.Font = value; OnSizeChanged(new EventArgs()); } } public System.Drawing.Color HelpColor { get { return helpLabel.ForeColor; } set { helpLabel.ForeColor = value; } } private System.Windows.Forms.Label helpLabel = new System.Windows.Forms.Label(); public System.Windows.Forms.Label HelpLabel { get { return helpLabel; } } } }