Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a Windows Forms control readonly?

Tags:

c#

.net

winforms

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?

like image 991
Gishu Avatar asked Nov 01 '08 18:11

Gishu


People also ask

How do you make a TextBox non editable in asp net?

You can make the TextBox as read-only by setting the readonly attribute to the input element.

What is a control in Windows Forms?

Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client-side, Windows-based applications. Not only does Windows Forms provide many ready-to-use controls, it also provides the infrastructure for developing your own controls.


2 Answers

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;
like image 93
Gulzar Nazim Avatar answered Sep 19 '22 17:09

Gulzar Nazim


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

like image 44
Rajan Arora Avatar answered Sep 22 '22 17:09

Rajan Arora