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!
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.
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 Form2Friend WithEvents Button3 As New MyButtonPrivate Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadButton3.Location = New Point(25, 75) Button3.Text = "Button3"Me.Controls.Add(Button3) End SubPrivate Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.ClickMe.Button3.Enabled = TrueEnd SubPrivate Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.ClickMe.Button3.Enabled = FalseEnd SubPrivate Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.ClickMessageBox.Show("hellow World") End Sub End Class Public Class MyButtonInherits ButtonProtected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs) If MyBase.Enabled ThenMyBase.OnPaint(pevent) MyBase.ForeColor = Color.BlueElseMyBase.OnPaint(pevent) Dim sf As SizeF = pevent.Graphics.MeasureString(Me.Text, Me.Font, Me.Width) Dim ThePoint As New PointThePoint.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 IfEnd SubEnd Class
|

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.