Reputation: 22233
I'm using a WinForms property grid to display the properties of an object. However, most of the properties are read only and thus show up as grey rather then black. Is there a way to customize the colors that are used? I'd like the disabled properties to be a bit easier to read.
BTW: I think the answer to this question might be related to what I'm trying to do. But I'm not sure exactly how I can access ControlPaint.DrawStringDisabled
.
Upvotes: 2
Views: 5040
Reputation: 11
Set the DisabledItemForeColor
property to ControlText
. This is the easiest way to make read-only properties appear the same as read-write properties.
Upvotes: 1
Reputation: 1
What sorcery is this? +1! I have seen other solutions that trap mouse and keyboard. This is by far the best and easiest solution. Here is a snippet of my inherited read-only control.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace MyCustomControls
{
public sealed class ReadOnlyPropertyGrid : System.Windows.Forms.PropertyGrid
{
#region Non-greyed read only support
public ReadOnlyPropertyGrid()
{
this.ViewForeColor = Color.FromArgb(1, 0, 0);
}
//---
private bool _readOnly;
public bool ReadOnly
{
get { return _readOnly; }
set
{
_readOnly = value;
this.SetObjectAsReadOnly(this.SelectedObject, _readOnly);
}
}
//---
protected override void OnSelectedObjectsChanged(EventArgs e)
{
this.SetObjectAsReadOnly(this.SelectedObject, this._readOnly);
base.OnSelectedObjectsChanged(e);
}
//---
private void SetObjectAsReadOnly(object selectedObject, bool isReadOnly)
{
if (this.SelectedObject != null)
{
TypeDescriptor.AddAttributes(this.SelectedObject, new Attribute[] { new ReadOnlyAttribute(_readOnly) });
this.Refresh();
}
}
//---
#endregion
}
}
Upvotes: 0
Reputation: 131
This problem has a simple solution.
Just reduce R in the RGB for forcolor of the PropertyGrid, like this:
Me.PropertyGrid2.ViewForeColor = Color.FromArgb(1, 0, 0)
This feature just acts on black color.
Upvotes: 13
Reputation: 8653
Unfortunately, there's no built-in way to change the colour. As with a lot of the standard .NET controls, they're merely wrapped versions of their COM equivalents.
What this means in practice is that a lot, if not all of the painting is done by the COM component, so if you try overriding the .NET OnPaint
method and calling ControlPaint.DrawStringDisabled
or any other painting code, it's more than likely going to have an undesired effect, or no effect what-so-ever.
Your options are:
WndProc
and try to intercept paint messages (not guaranteed to work)OnPaint
and do your painting on top (not guaranteed to work)Sorry that's probably not the answer you wanted, but I can't think of an easier method. I know from bitter experience that this sort of thing can be hard to modify.
Upvotes: 0