none
How to make textbox readonly?

    Question

  • When i drag a textbox from toolbox in visual studio by default its read only property is false.
    is there any way to make it true when i drag it from toolbox in visual studio 2005.
    Sharad Sharma Pursuit Technologies Bangalore
    Saturday, October 10, 2009 2:15 PM

Answers

  • If you are asking whether Visual Studio provides a way to set the default values for these properties, the answer is no.

    A relatively easy solution might be to create an inherited TextBox:

    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        // A TextBox that is ReadOnly by default. ReadOnly can still be set to False later.
        class ReadOnlyTextBox : TextBox
        {
            public ReadOnlyTextBox()
            {
                this.ReadOnly = true;
            }
    
            // The following is designer support so that the designer will realize that "true"
            // is now to be considered the default value for this property. This affects, for example,
            // when code serialization is performed and how the property Reset feature works. 
    
            [DefaultValue(true)]
            public new bool ReadOnly
            {
                get
                {
                    return base.ReadOnly;
                }
                set
                {
                    base.ReadOnly = value;
                }
            }
        }
    }
    


    Once you add this to your project, rebuild so the control will appear in the toolbox.
    • Marked as answer by Sharad Kumar Sunday, October 11, 2009 3:18 PM
    Saturday, October 10, 2009 2:26 PM

All replies

  • If you are asking whether Visual Studio provides a way to set the default values for these properties, the answer is no.

    A relatively easy solution might be to create an inherited TextBox:

    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        // A TextBox that is ReadOnly by default. ReadOnly can still be set to False later.
        class ReadOnlyTextBox : TextBox
        {
            public ReadOnlyTextBox()
            {
                this.ReadOnly = true;
            }
    
            // The following is designer support so that the designer will realize that "true"
            // is now to be considered the default value for this property. This affects, for example,
            // when code serialization is performed and how the property Reset feature works. 
    
            [DefaultValue(true)]
            public new bool ReadOnly
            {
                get
                {
                    return base.ReadOnly;
                }
                set
                {
                    base.ReadOnly = value;
                }
            }
        }
    }
    


    Once you add this to your project, rebuild so the control will appear in the toolbox.
    • Marked as answer by Sharad Kumar Sunday, October 11, 2009 3:18 PM
    Saturday, October 10, 2009 2:26 PM
  • Hi,

    The code snippets of BinaryCoder is excellent... You can also set the readonly property value as true. 
    Md. Marufuzzaman
    Saturday, October 10, 2009 2:53 PM
  • Or just set the Enabled item to false. It will only result in some more 'grey' textbox, but I use it :).
    Saturday, October 10, 2009 3:13 PM
  • Enabled also stops the user from scrolling and from using the builtin Copy to clipboard command.  For these reasons, I like to avoid it.

    Saturday, October 10, 2009 3:30 PM
  • A text box that is always ReadOnly is a Label.  Use a Label.  You might want to change the label's AutoSize, BackColor and BorderStyle properties.

    Hans Passant.
    Saturday, October 10, 2009 3:38 PM
    Moderator
  • > A text box that is always ReadOnly is a Label.  Use a Label.

    Disagree.... See my comment about scrolling and copying.
    Saturday, October 10, 2009 4:59 PM
  • Noted.  Scrolling and copying are distinct requirements.

    Hans Passant.
    Saturday, October 10, 2009 5:02 PM
    Moderator