tags:

views:

64

answers:

5

In winforms .net controls if we set Enabled property to false the control will be grayed out.

In this case it will become unreadable for many color combinations (because i am giving options to change color of a form for user at run time).

I can use ReadOnly property but it is available only to TextBox controls not for other controls like ComboBox, DateTimePicker etc.

I just wanted to know is there any option available so that i can prevent controls from graying out when it is disabled.

+1  A: 

Realistically you do not want to modify the color behaviours of disable/enable controls. Hit up Microsoft's updated Windows UX Guidelines when you get a chance as it will give you some guidance to layout/design, but if this is a absolutely must feature for your application then you will need to either handle the controls paint event yourself, or inherit and override its paint event and draw the custom colors on your own to control this aspect completely.

My personal recommendation would to be find another avenue, as others have mentioned people expect programs to behave a specific way, however if you do provide color scheme features, maybe limit the parts of the application that can be changed/personalized.

David Anderson
+1  A: 

I will go out on a limb and guess that you need to make certain fields read-only based on the user's access rights. Meaning that a user with right to edit certain information might see the combobox while a user laking this right would not be allowed to edit the information but can still view it and possibly needs to view it.

I would suggest that you have the screen select the right control based on the requirement. If the user can change the selected value in a combobox, show the combobox. If the user should not be allowed to change the value, show a readonly textbox which contains the selected value.

One way to simplify the above solution is to develop some user controls that adjust the way they display the data according to a propery, lets call it Editable, of the control. So you can create a user control which shows a combobox if Editable is true and a textbox if Editable is false. And then a corresponding control for the datatimepicker etc.

Chris Taylor
Not exactly, its like disabling the entire form. It has to be enabled only user triggers an modify record option.
Prasad
@Prasad, in this case I see two options. 1- You can still apply the approach I describe, toggling the Editable property on the controls when the user switches to edit mode. 2- You can create a form for viewing the data and a second form for the edit process and show the second form when the user selects to edit the data.
Chris Taylor
+1  A: 

This is a sad moment in most any usability study, seeing the subject banging away at the mouse and keyboard and not understanding why it doesn't work. But you can get it if you really want to. The trick is to put a picture box in front of the control that shows a bitmap of the controls in their previously enabled state. They'll never figure out they are clicking on a bitmap instead of the actual controls.

Best done with a Panel so you can easily disable controls as a group. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. And put the controls inside it that should be disabled. Everything else is automatic, just set the Enabled property to false and the user won't know what happened:

using System;
using System.Drawing;
using System.Windows.Forms;

class FakeItPanel : Panel {
    private PictureBox mFakeIt;

    public new bool Enabled {
        get { return base.Enabled; }
        set {
            if (value) {
                if (mFakeIt != null) mFakeIt.Dispose();
                mFakeIt = null;
            }
            else {
                mFakeIt = new PictureBox();
                mFakeIt.Size = this.Size;
                mFakeIt.Location = this.Location;
                var bmp = new Bitmap(this.Width, this.Height);
                this.DrawToBitmap(bmp, new Rectangle(Point.Empty, this.Size));
                mFakeIt.Image = bmp;
                this.Parent.Controls.Add(mFakeIt);
                this.Parent.Controls.SetChildIndex(mFakeIt, 0);
            }
            base.Enabled = value;
        }
    }

    protected override void Dispose(bool disposing) {
        if (disposing && mFakeIt != null) mFakeIt.Dispose();
        base.Dispose(disposing);
    }
}
Hans Passant
Thanks, The above solutions works fine. But what if in disabled mode if i assaign some value to control at run time? It will not update since it is image of panel. Is there any way to handle it?
Prasad
A: 

Don't. Just don't.

Lucas
A: 

If you have a "read only mode" in your program, make checkboxes, optionboxes, listboxes and textboxes as labels.

I have for example done an online quiz that when entering values, its built of checkboxes and so on, but when someone review the quiz you see all answers as labels with the selected value as text. For me this is the only way to do it without disturbing the normal way of thinking UI both for the user and developer.

If you have a group of selectable choices and want the choices to be visible even in the read only mode, then write out all choices and mark out the selected one in a way that does not look like a optionbox och checkbox.

Thats my 5 cents in this area.

Stefan