We help IT Professionals succeed at work.

VB.Net Bound Form set Read Only Dynamically

BorisMatthews
on
Medium Priority
784 Views
Last Modified: 2012-05-12
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?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2015

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

textBox1.Attributes.Add("readonly", "true")

Author

Commented:
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.
CERTIFIED EXPERT
Top Expert 2015

Commented:
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?

Author

Commented:
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...
CERTIFIED EXPERT
Top Expert 2015
Commented:
That sounds more like an event handling issue rather than whether the control is read only or editable. It doesn't matter that the controls are bound to the data table. Just bypass your Click event handler for those controls when a read only user is using the app.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
There is an AllowEdit property on bindingsource. Set it to false

http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.allowedit.aspx

Author

Commented:
But that is in itself a read only property and has no Set procedure...
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:

Author

Commented:
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...
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Another option is to enclose the tableadapter.update statement in an If block and only execute it if the user has permission to make changes. Or may be hide the save button?

Author

Commented:
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...