1
1

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.

今更ながらVB.NET2005を使ったアプリケーションでテキストボックスとコンボボックスがEnabledの時の文字色を変える必要が出てきたので調査した件

Posted at

前提としてEnabled時は文字色が灰色になります。そして、Enabled時の文字色を変えるプロパティは存在しません。(現在最新の2013でもないっぽい。何故無いのだろう。)
よって、通常のテキストボックスでEnabled時で文字色を変えたいならば、Enabledの代わりに、ReadOnlyをTrueに、TabStopをFalseにして擬似的なEnabled状態を作る方法がまず一点。ただし、コンボボックスにはReadOnlyが存在しないので、動きに一貫性が無くなる点が問題。

で、次の解決策、テキストボックスのサブクラスを作って、その中でEnabledでの動きをオーバーライドしてEnabled時の文字色を変更するというもの。今回はこちらを採用。

リンク先のサンプルソース上にあるペイントのオーバーライド時に色を編集しているので好みの色を設定してやればいい。

MyTextBox
'TextBoxを描画する
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
        Dim b As New System.Drawing.SolidBrush(Me.ForeColor)
        '文字列を描画する
        e.Graphics.DrawString(Me.Text, Me.Font, b, -1, 1)
        b.Dispose()
    End Sub

で、この解決策はコンボボックスでも適用できます。

MyComboBox
Class MyComboBox
    Inherits ComboBox
'以下同じ

ただし、ComboBoxの場合、Enabled時に右にある三角のやつが消えます。再描画が微妙にうまく出来てないということか?そこは解決方法があるかもしれませんが、今回はパス。誰か知ってるなら教えて下さい。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?