Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I change the font color on a disabled textbox 1

Status
Not open for further replies.

aprobe

Programmer
Dec 4, 2003
36
NZ
I have disabled text boxes which are greyed out and I have no control of the font color.

I can do something like
c.ForeColor = Color.Red;

which works fine with enabled textboxes but is totally ignored on disabled boxes which remain a messy murky grey.

Thanks

 
Is this Wondows forms and could you use the ReadOnly property rather than the disabled Property for simplicity sake?


Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
Great, It does what I want.

I never thought of setting ReadOnly, I've always tended to use Enable/Disable.

I still would be interested to know how to change the ForeColor of disabled items.
 
I've seen, (and have done), similar with other controls when I've had a need to extend their functionality. Below is how I implemented what you want, (in a very basic way), with a DateTimePicker control which I'm sure you can improve on and apply to a TextBox if you've an interest too.
Code:
#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Runtime.InteropServices;

#endregion

namespace SludgeSchedulerControls.Widgets
{
	class ExtendedDateTimePicker : System.Windows.Forms.DateTimePicker
	{
		private System.Drawing.Color _backColorDisabled = System.Drawing.Color.Gainsboro;// System.Drawing.Color.Gainsboro;
		private System.Drawing.Color _foreColorDisabled = System.Drawing.SystemColors.ControlText;

		protected override void OnEnabledChanged( EventArgs e)
		{
			base.OnEnabledChanged( e );
			if (!(this.Enabled))
			{
				this.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true);
			}
			else
			{
				this.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, false);
			}

			this.Invalidate();
		}

		protected override void OnPaint( System.Windows.Forms.PaintEventArgs e )
		{
			base.OnPaint( e );
			System.Drawing.SolidBrush textBrush;

			//Set user selected backcolor when disabled
			if (this.Enabled)
			{
				textBrush = new System.Drawing.SolidBrush( this.ForeColor );
			}
			else
			{
				System.Drawing.Color backColorDisabled = this._backColorDisabled;
				if (this.Parent.FindForm() != null)
				{
					backColorDisabled = this.Parent.FindForm().BackColor;
				}
				textBrush = new System.Drawing.SolidBrush( this._foreColorDisabled );
				System.Drawing.SolidBrush backBrush = new System.Drawing.SolidBrush( backColorDisabled );
				e.Graphics.FillRectangle( backBrush, ClientRectangle );
			}

			//Paint text
			e.Graphics.DrawString( this.Text, this.Font, textBrush, 1.0F, 1.0F );
		}
	}
}

Hope this helps :)

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
Many thanks for your help on this.

I'll print this off for future use.

[thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top