简体   繁体   中英

How do I make a Windows Forms control readonly?

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?

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;

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

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.

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);
   }
}

I was given this same requirement at work yesterday. Except instead of a textbox I had to make an entire form disabled without changing it's color.

So I replaced a call to

form->Enabled = false;

with

IntPtr hWnd = form->Handle;
HWND window_handle = (HWND)hWnd.ToPointer();
::EnableWindow(window_handle, aEnable ? TRUE:FALSE);

Which worked well. You can see above that I am using managed C++. The entire form is now disabled, but not greyed out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM