tags:

views:

1683

answers:

5

Returning to WinForms in VS2008 after a long time.. Tinkering with a OOD problem in VS2008 Express Edition.

I need some controls to be "display only" widgets. The user should not be able to change the value of these controls... the widgets are updated by a periodic update tick event. I vaguely remember there being a ReadOnly property that you could set to have this behavior... can't find it now.

The Enabled property set to false: grays out the control content. I want the control to look normal. The Locked property set to false: seems to be protecting the user from accidentally distorting the control in the Visual Form Designer.

What am I missing?

+11  A: 

For some typical winforms controls:

http://jquiz.wordpress.com/2007/05/29/c-winforms-readonly-controls/

This is also a good tip to preserve the appearance:

    Color clr = textBox1.BackColor;
    textBox1.ReadOnly = true;
    textBox1.BackColor = clr;
Gulzar
Aah.. Readonly was for plain textboxes. Radiobuttons are easy too. I got one combobox that would need an ugly hack.. On the other hand if its only readonly.. I might as well turn that into a label or textbox. Thanks will accept this one unless we have a new contender.
Gishu
i agree with Jonathan but just added that tip if you really need it.
Gulzar
+2  A: 

Textbox

.ReadOnly property to true

Controls without ReadOnly

Other control do not have all the time the ReadOnly property. You will require to play with the Events to take off the editing process and keeping your value not editable.

Daok
+4  A: 

I want the control to look normal.

I'd just like to point out that it is a horrible UI experience to have a read only control that appears the same as an editable control.

FlySwat
Point noted. It's a fake/test app to just demo out something.. I think the UX Gods will let me live :)
Gishu
On second thoughts I think Images would be the best answer.. my form just shows the state of different valves on a real time basis. Each widget needs to show a different image based on a backing field value
Gishu
A: 

Two relevant properties ReadOnly and Enabled. ReadOnly = true prevents editing grays out the background, but it still allows focus. Enabled = false grays out the background, text and prevents editing or focus.

Windows UI conventions dicate giving the user a visual cue that a control is readonly (that way they won't attempt to edit it and be subsequently frustrated). The grayed out disabled state is the defined system convention, but it's arguable too much of a cue (and not a legibile enough one).

The simplest route is probababy to set your control to ReadOnly, set the background to System.Drawing.SystemColors.Window and then block focus messages. You could do this by catching OnEnter events and immediately moving Focus to another control that's not readonly (say, a Close or Edit button). Or you could derive your own control and eat any WM_SETFOCUS messages. Example below.

I believe various third-party control sets give you additional options and granularity.

public class ReadOnlyTextBox : TextBox
{
   const uint WM_SETFOCUS = 0x0007;

   public ReadOnlyTextBox()
   {
      this.ReadOnly = true;
      this.BackColor = System.Drawing.SystemColors.Window;
      this.ForeColor = System.Drawing.SystemColors.WindowText;
   }

   protected override void WndProc(ref Message m)
   {
      // eat all setfocus messages, pass rest to base
      if (m.Msg != WM_SETFOCUS)
         base.WndProc(ref m);
   }
}
Grant
A: 

To make the forms control Readonly instantly on one click do use the following peice of Code :

    public void LockControlValues(System.Windows.Forms.Control Container)
    {
        try
        {
            foreach (Control ctrl in Container.Controls)
            {
                if (ctrl.GetType() == typeof(TextBox))
                    ((TextBox)ctrl).ReadOnly = true;
                if (ctrl.GetType() == typeof(ComboBox))
                    ((ComboBox)ctrl).Enabled= false;
                if (ctrl.GetType() == typeof(CheckBox))
                    ((CheckBox)ctrl).Enabled = false;

                if (ctrl.GetType() == typeof(DateTimePicker))
                    ((DateTimePicker)ctrl).Enabled = false;

                if (ctrl.Controls.Count > 0)
                    LockControlValues(ctrl);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

Then call it from your Button Click Event like this :

LockControlValues(this)

Hope, this helps to solve your problem :

Happy Programming,

Rajan Arora www.simplyrajan.co.nr

Rajan Arora