こんにちは。
フォーム上のボタンを、条件によっては使用不可にしたいと思っています。
その場合、ボタンのEnabledプロパティをFalseにすればボタンがクリック
できずに使用不可となりますが、この時にボタンの色やテキストの文字の
色等を変更したいのです。
ボタン使用不可にする場合に実現したいことは、
・ボタンクリックに対しては、ボタンのEnabledプロパティをFalseにした場合と
同様のふるまいをする。
(ボタンが沈まない。ピクチャと同じような感じでもよい)
・ボタン使用不可時のボタンの文字色・背景色を任意に設定できる。
(ボタンのEnabled = Falseの場合、ボタンの背景が薄いグレーになり、
文字色も濃いグレーになりますが、指定した色にしたいのです。)
何かいい方法はないでしょうか?
宜しくお願いします。
なお、開発環境はVB2008です。
>フォーム上のボタンを、条件によっては使用不可にしたいと思っています。
>その場合、ボタンのEnabledプロパティをFalseにすればボタンがクリック
>できずに使用不可となりますが、この時にボタンの色やテキストの文字の
>色等を変更したいのです。
パネルの中にボタンを入れ、パネルのEnabled = Falseではどうでしょうか?
http://dobon.net/vb/dotnet/control/btnownerdraw.html
ここまでは理解できてるのですよね
みなさん、回答ありがとうございます。
matsuさん
>パネルの中にボタンを入れ、パネルのEnabled = Falseではどうでしょうか?
この方法だと、ボタン内の文字がグレーになってしまいます。
また、ボタンの輪郭が薄くなってしまいます。
kidさん
>http://dobon.net/vb/dotnet/control/btnownerdraw.html
>ここまでは理解できてるのですよね
ここでテキストを描画すると、微妙にボタンテキストがずれているようで、
元のテキストとずれて重なって表示されます。
また、Enabled = Falseにすると、前出と同じようにボタンの輪郭が薄くなってしまいます。
ボタンの輪郭を通常と同じように表示させるにはどうしたらよいでしょうか?
宜しくお願いします。
デザインはButtonが2個、(1つはカラー選択用)、 TextBox1が1個です
TextBox1に色名を入力、Button2で ColorDialogを表示して選択
Button1をクリックするとButton1に選択した色文字がでます。
このときTextBox1とButton2が画面上から消えます
再度Button1をクリックすると入力した文字が消えてTextBox1とButton2が画面上に表示されます。
こんなんでどうでしょうか?
Public Class Form1
Dim frag As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frag = Not frag
If frag = True Then
Button1.Enabled = True
Button2.Visible = True
Button1.Text = ""
Else
Button1.Enabled = True
Button2.Visible = False
TextBox1.Clear()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim MyDialog As New ColorDialog()
Dim s As String
MyDialog.AllowFullOpen = False
MyDialog.ShowHelp = True
MyDialog.Color = Button1.ForeColor
s = TextBox1.Text
If (MyDialog.ShowDialog() = DialogResult.OK) Then
Button1.ForeColor = MyDialog.Color
Button1.Text = s
End If
End Sub
End Class
修正しました。
Public Class Form1
Dim frag As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frag = Not frag
If frag = True Then
Button1.Enabled = True
Button2.Visible = True
Button1.Text = ""
TextBox1.Visible = True
Else
Button1.Enabled = True
Button2.Visible = False
TextBox1.Clear()
TextBox1.Visible = False
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim MyDialog As New ColorDialog()
Dim s As String
MyDialog.AllowFullOpen = False
MyDialog.ShowHelp = True
MyDialog.Color = Button1.ForeColor
s = TextBox1.Text
If (MyDialog.ShowDialog() = DialogResult.OK) Then
Button1.ForeColor = MyDialog.Color
Button1.Text = s
End If
End Sub
End Class
再度修正しましたが、貴殿の考えているのとは違うような気もしますが
Public Class Form1
Dim frag As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frag = Not frag
If frag = True Then
Button2.Visible = True
Button1.Text = TextBox1.Text
TextBox1.Visible = True
Else
Button2.Visible = False
TextBox1.Clear()
TextBox1.Visible = False
End If
PictureBox1.Left += 5
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim MyDialog As New ColorDialog()
Dim s As String
MyDialog.AllowFullOpen = False
MyDialog.ShowHelp = True
MyDialog.Color = Button1.ForeColor
s = TextBox1.Text
If (MyDialog.ShowDialog() = DialogResult.OK) Then
Button1.ForeColor = MyDialog.Color
Button1.Text = s
End If
End Sub
End Class
文字と背景色を同時変更できるようにしました。
Public Class Form1
Dim frag As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frag = Not frag
If frag = True Then
Button2.Visible = True
Button1.Text = TextBox1.Text
TextBox1.Visible = True
Else
Button2.Visible = False
TextBox1.Clear()
TextBox1.Visible = False
End If
'PictureBox1.Left += 5
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim MyDialog As New ColorDialog()
Dim mydialog2 As New ColorDialog()
Dim s As String
MyDialog.AllowFullOpen = False
mydialog2.AllowFullOpen = False
MyDialog.ShowHelp = True
mydialog2.ShowHelp = True
MyDialog.Color = Button1.ForeColor
mydialog2.Color = Button1.BackColor
s = TextBox1.Text
If (MyDialog.ShowDialog() = DialogResult.OK) Then
Button1.ForeColor = MyDialog.Color
If (mydialog2.ShowDialog() = DialogResult.OK) Then
Button1.BackColor = mydialog2.Color
Button1.Text = s
End If
End If
End Sub
End Class
ボタンの外観をピクチャーボックスに表示してみました。
フォームにボタン2個とピクチャーボックス1個用意してください。
Button1より前面にピクチャーボックスが表示されるようにしておいてください。
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim bmp As New Bitmap(Button1.Width, Button1.Height)
Button1.DrawToBitmap(bmp, New Rectangle(0, 0, Button1.Width, Button1.Height))
PictureBox1.Image = CType(bmp.Clone, Image)
PictureBox1.Size = Button1.Size
PictureBox1.Location = Button1.Location
bmp.Dispose()
End Sub
>ボタン使用不可にする場合に実現したいことは、
>・ボタンクリックに対しては、ボタンのEnabledプロパティをFalseにした場>合と 同様のふるまいをする。
> (ボタンが沈まない。ピクチャと同じような感じでもよい)
>・ボタン使用不可時のボタンの文字色・背景色を任意に設定できる。
>(ボタンのEnabled = Falseの場合、ボタンの背景が薄いグレーになり、
>
>文字色も濃いグレーになりますが、指定した色にしたいのです。)
最終的に、どのようなプログラムを作ろうとされているのかを
おしえていただけますか?
ライナさんの考えているプログラムは作成不可能と判断しましたが
識者の方はどのように考えられますか(後学のためにおしえてください)
アクアと申します。どうぞ宜しくお願いします。
ライナさんが提示された条件とは多少、異なりますが、
Buttonコントロールを継承したコントロールを作成すれば希望に近いことは可能でしょう。
Buttonを継承したコントロールを追加する手順は省きますが、そこに書き加えるコードを示します。
Private IsClick As Boolean = True ' Enabledに代わる独自のフラグ
Private cfColor As Color = System.Drawing.SystemColors.ControlText
Private cbColor As Color = System.Drawing.SystemColors.Control
Property IsAcceptClick() As Boolean
Get
Return IsClick
End Get
Set(ByVal value As Boolean)
IsClick = value
Select Case value
Case True
Me.ForeColor = _
System.Drawing.SystemColors.ControlText
Case False
Me.ForeColor = Me.CForeColor
End Select
Me.Refresh()
End Set
End Property
Public Property CForeColor() As Color
Get
Return cfColor
End Get
Set(ByVal value As Color)
cfColor = value
Me.Refresh()
End Set
End Property
Public Property CBackColor() As Color
Get
Return cbColor
End Get
Set(ByVal value As Color)
cbColor = value
Me.Refresh()
End Set
End Property
上記の感じでButtonコントロールを継承したカスタムコントロールを作って一度ビルドすればFormデザイナの画面のツールボックスウィンドウに作成したカスタムコントロールが表示されているはずです。
そのカスタムコントロールをフォームにドロップして、コードデザイナのそのコントロールのクリックイベントの1行目に
If AquaButton1.IsAcceptClick = False Then Exit Sub
と記述すれば、ほぼご希望の事が可能でしょう。
だいぶ手順の説明を省きましたが、カスタムコントロールを追加する手順は
http://msdn.microsoft.com/ja-jp/events/dd229370.aspx
が参考になるかと存じます。
アクアさん、ありがとうございます。
いますぐには理解しかねますので、じっくり勉強させていただきます。
有り難うございました。
> If AquaButton1.IsAcceptClick = False Then Exit Sub
ごめんなさい。
AquaButton1ってなってますが、カスタムコントロールの名前です。
ツイート | ![]() |