views:

380

answers:

2

I created a Data Source with VB.NET and Visual Studio 2005. I dragged the data source onto my dialog, and VS created the text boxes with the members of my linked Object, a System.Windows.Forms.BindingSource and a System.Windows.Forms.BindingNavigator.

I populate the List (called myList), set myList as the DataSource in the BindingSource, and things work peachy except for the fact that I want this to be read-only. If the user changes something in one of the text boxes, it saves the changes.

I tried creating a read-only collection to bind to the BindingSource, but that didn't solve the problem:

Dim detailsDlg As New dlgMyDetails
Dim readOnlyList As New ReadOnlyCollection(Of MyObjects)(myList)

detailsDlg.MyBindingSource.DataSource = readOnlyList
detailsDlg.ShowDialog()

I guess I could disable all of the text boxes, but that seems a bit heavy-handed, plus I'd probably want to change the font color so that it's easier to read.

Ideally, I probably wouldn't care if users were able to set focus to the text boxes, or even edit the contents, but I just wouldn't want any changes to persist. That is, if someone edited something, used the navigator to go to the next record, and then returned, I'd want it as it was before the user played with it.

Any suggestions / guidance?

Thanks in advance!

+1  A: 

Instead of making a ReadOnlyCollection you can change the property in your class (MyObjects) to ReadOnly or add attribute System.ComponentModel.ReadOnly(true) to your property, example:

class Person
{
    public Person(int id, string name, string address)
    {
        _id = id;
        Name = name;
        Address = address;
    }

    private int _id = 0;
    public int ID { get { return _id; } }

    [System.ComponentModel.ReadOnly(true)]
    public string Name { get; set; }

    public string Address { get; set; }
}

ID and Name is going to be readonly, sorry if the example is in C#. Hope this helps.

Cheers.

dkartopr
Thanks very much for responding dkartopr. Does this force all instances of Person to be read-only? In general I'll need the ability to read and write to these objects.
John at CashCommons
using the System.ComponentModel.ReadOnly(true), yes it will apply to all instances of Person. Say for example, you have 1 text box which bind to Name property, changes made to the text box will not be reflected to the current instance of Person.On the other hand, when you do: instance.Name = "foo", this will change the instance name to "foo".
dkartopr
Oh, so I can change read-only instances of Person programmatically? I was assuming that I could construct them, but then the values were set in stone (much like a C++ "const" member). (Note: I guess that would make sense, since your class defines both get and set.)
John at CashCommons
+1  A: 

From a Model-View-Control perspective, the constraint you want is not on the model or control, but the view. The view should restrict what is editable on the screen.

If it truly is read-only, why not go with a read-only user interface element, ie, a label? The reason you do this is to reduce confusion to the user. If it is a textbox, there is a reasonable expectation that at some point the data becomes editable. If this is not the case, then presenting a disabled textbox is likely not the right UI element to present, rather, as mentioned, a label.

Wayne Hartman
Very interesting idea! But I take it that I'd be rolling my own view this way, correct? I can get a DataGridView or a collection of TextBox controls in about three seconds by dragging a DataSource onto the form. It's probably not this easy to do a set of labels?
John at CashCommons
@John at CashCommons - 1) You can roll your own, or 2) you can use a DataGridView and disable editing. 3) You can create a collection of Label controls just as easy as a collection of TextBox controls. Remember, Textboxes and Labels inherit share a common parent object, Control.
Wayne Hartman
I don't have VS 2005 right in front of me now, but can I modify the drag-and-drop capability that the IDE provides for DGVs and for TextBoxes to give me Labels instead? That would probably be the best way I could go, but if I have to roll up my sleeves a bit to get there I guess it won't kill me to learn something new. :)
John at CashCommons