Changing Disabled Controls ForeColor

Hi,

Is there an "easy" way to change a control's ForeColor when it is diabled? Yes, I know that this goes against the Windows standard.

On our forms, we have buttons with a dark blue background and a white foreground (the design is dictated by marketing). When the buttons become disabled, the ForeColor goes to a dark color making the button unreadable.

Thanks!

[399 byte] By [JamesA.Gayhart] at [2007-12-29]
# 1

I hate those marketing dictators too. They'll drive you nuts and it seems that they're always Mac users.

I had a similar situation in a VB6 app and there we used the graphical style of the button to achieve the desired effect. However, this feature isn't in VB.NET although this article describes how to get a similar effect: How to: Emulate a Visual Basic 6.0 Tri-state Control in an Upgraded Application

Another option is to create your own button control or subclass the standard button control with the behavior you want. This is a little more involved but I found this flexible approach to work well in another situation where a marketing group and an operations group kept fighting over how to 'skin' their in-store application. Just do a help search on "subclass button" and you'll find several examples to get you started.

FrankCarr at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

For Each c As Control In Me.Controls

If Not c.Enabled Then

c.ForeColor = Color.Blue

End If

Next

DMan1 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

Hi,

Unfortunately, that does not change the ForeColor when the control is disabled. Or least it doesn't change a button's ForeColor when disabled (which is what I'm trying to do).

JamesA.Gayhart at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Ah, gotcha...for that the best method is to create your own button (control) the inherits from the base and then do your own painting...here is your solution for the button...this custom button turns the text red when it is disabled:


Public Class Form2

Friend WithEvents Button3 As New MyButton

Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Button3.Location = New Point(25, 75)
Button3.Text = "Button3"

Me.Controls.Add(Button3)
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.Button3.Enabled = True

End Sub

Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

Me.Button3.Enabled = False

End Sub

Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click

MessageBox.Show("hellow World")
End Sub
End
Class
Public
Class MyButton

Inherits Button

Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
If MyBase.Enabled Then

MyBase.OnPaint(pevent)
MyBase.ForeColor = Color.Blue

Else

MyBase.OnPaint(pevent)
Dim sf As SizeF = pevent.Graphics.MeasureString(Me.Text, Me.Font, Me.Width)
Dim ThePoint As New Point

ThePoint.X = (Me.Width / 2) - (sf.Width / 2)
ThePoint.Y = (Me.Height / 2) - (sf.Height / 2)
pevent.Graphics.DrawString(Me.Text, Me.Font, Brushes.Red, ThePoint)
End If

End Sub

End Class


DMan1 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

Hi,

That helps a lot. Thanks. Of course, I would prefer to not do it. Maybe we can get Marketing to change their minds.

JamesA.Gayhart at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

Hi,

So I was halfway through testing my new custom button class when I ran across the BackgroundImage property. It turns out that if I use a default button, set the background image to an image containing the backcolor I want, and change the ForeColor to something appropriate, everything works like I want to.

JamesA.Gayhart at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...