8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Windows Form のボタン背景色変更とEnabled=falseの振る舞い

Last updated at Posted at 2015-04-05

ツマラナイことで時間がかかったので、まとめておこうとおもいました。
今後 WinForm 使うことも少なくなるとは思うのではありますけれども。

やりたかったこと

・Buttonに背景色をつける
・Buttonは状況に応じて有効(Enabled=true)、無効(Enabled=false)の状態がある
・Buttonの表示文字はボタンの有効/無効に合わせて表示色が変化する

困ったこと

・Enabled=false にしても、Buttonに表示している文字色が「黒」のまま
 Buttun自体は期待通り、無効表示になっている

解決方法

・Enabled=false にする際、一緒に Butotn.BackColor = System.Drawing.SystemColors.Control にすること。これでButtonに表示している文字色が期待通りに変化する。

code

code.cs

// 背景色を変更します
button1.BackColor = Color.BurlyWood;

// ボタンを無効にします
button1.Enabled = false;
// ボタンの背景色をシステム指定に戻します
button1.BackColor = System.Drawing.SystemColors.Control;

8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?