Link to home
Start Free TrialLog in
Avatar of BorisMatthews
BorisMatthews

asked on

VB.Net Bound Form set Read Only Dynamically

I have a VB.Net form bound to a DataSource via a TableAdaptor that displays information and depending upon the user logged in should be either enabled for edits or for other users read only.

On the load of the form I can quite easily set all the fields to be Enabled = False but this does not look good for the read only user.

Is there a way of making all controls (combo box, lisy box, text box etc) read only or is there a way of dynamically setting the bindingsource object to be read only?
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

In code behind you could add the readonly attribute to the controls based on a conditional.

textBox1.Attributes.Add("readonly", "true")
Avatar of BorisMatthews
BorisMatthews

ASKER

Are you saying that I can add this attribute to combo boxes and list boxes also?

I have implemented an interim solution whereby all text boxes have readonly set to True and then all combo boxes and list boxes have enabled set to false but this is not ideal.
Sorry, I'm so used to working with Web forms rather than Windows forms.

You have the solution for textboxes. List boxes are not editable by default. Comboboxes can be made to behave like DropDownLists where the items are select-able but not editable by setting the DropDownStyle.

Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

Does that cover it?
No this does not.  Because the controls are bound to the data table if a read only user selects a new combo box or list box value this will be updated in the table...
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But that is in itself a read only property and has no Set procedure...
I could not get the AllowEdit feature to stop the data being changed, not sur why as I followed the examples.

I have investigated further the suggestion to handle the click event and in fact for combo boxes I have created an event handler for all combo boxes on the form - their Enter event - and changed the focus to a text box that is read only.

This works well for combo boxes but could not get this to work for check boxes.  The focus moved OK but this did not stop the check state of the checkbox changing...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good idea to ensure no updates via trapping the update statement for a guest user which I have done.  I have used a mix of suggestions from the answers given and so will share the points.  The solutions works OK but is not ideal as really it would be good to have a visual way of showing the user that they are read only without having to disable some of the controls...