locked
How do I make a form read only (vb.net) RRS feed

  • Question

  • Hi,

    I'm aiming to create a form to display non editable information. I am aware that I can set the enabled property to false, however this greys out the information and I dont want this.

    Is there any way I can make the form (or the controls on it) read only?

    Thanks

     

    Thursday, January 11, 2007 4:48 PM

Answers

  • Hi,

    Try this...

    For Each ctrl As Control In Me.Controls

    If TypeOf (ctrl) Is TextBox Then

    CType(ctrl, TextBox).ReadOnly = True

    End If

    Next

    Friday, January 12, 2007 11:12 AM

All replies

  •  

    You can use readonly property of textbox to make it readonly

    public bool ReadOnly { get; set; }
    In case of dropdown, you can have only one item when you wanted it to be readonly.
    Thursday, January 11, 2007 5:00 PM
  • Sorry for my ignorance (im new to .net!) but how do I use this code? Do I need to create a class??
    Thursday, January 11, 2007 5:25 PM
  •  

    Well, as your aim is to create form for only display, you can use labels to show the value.

    If you want to make TextBox control as readonly, you need to set textbox's ReadOnly property to true.

    Hope this helps you.

    Thursday, January 11, 2007 5:54 PM
  • You can set ReadOnly at design-time. The TextBox should have it as a setable property.
    Thursday, January 11, 2007 5:56 PM
  • Sorry, I should have been clearer in explaining what I am trying to do:-

    I have a form which I want to be able to open in 2 scenarios 1. For data input,  2. for Viewing Only, therefore I do not want to set the properties at design time, I want to set them when the form is opened for viewing only.

    Is this possible?

     

    Thanks Again!

     

    Friday, January 12, 2007 9:47 AM
  •  

    You can change it at run time too.

    in your code you have to set the ReadOnly property to true

    e.g. if your textbox name is textBoxFirstName then on view only condition  you have to set following

    textBoxFirstName.ReadOnly = true

     

    Friday, January 12, 2007 10:07 AM
  • Thanks for the info!

    Is there any way I can loop through the controls and set them all to  read only rather than having to set each controls property individually?

    I have tried "for each ctl as control..... "etc, but read only isnt available.

    Friday, January 12, 2007 10:23 AM
  • Hi,

    Try this...

    For Each ctrl As Control In Me.Controls

    If TypeOf (ctrl) Is TextBox Then

    CType(ctrl, TextBox).ReadOnly = True

    End If

    Next

    Friday, January 12, 2007 11:12 AM
  • Brilliant!, That worked!!

    Thanks for all your help!

    Friday, January 12, 2007 5:17 PM