views:

2264

answers:

4

I am trying to disable a number of text boxes intended for displaying data (not edit) in one of my UserControls. However, for some reason I can not get the textBoxes to disable properly.

I've set "ApplyAuthorization on readWriteAuthorization" to true and the textBoxes are databound to the correct properties.

I've also added the following lines to the CanWriteProperty of my object:

if (propertyName == OpeningDateProperty.Name) return false;
if (propertyName == ChangeDateProperty.Name) return false;
if (propertyName == CloseDateProperty.Name) return false;
return base.CanWriteProperty(propertyName);

I can't figure out what I'm doing wrong here. I've implemented pretty much the same thing recently in other UserControls without any problems...

I am using Windows Forms in C# .NET (Visual Studio 2008)

EDIT: The code snippets and the properties are taken from my customer object. The date represent opening, last change and closure of the customer account. They are never supposed to be edited at all and in fact in the old sollution they are represented by textLabels, however we now want to use a text box and make the property's CanWriteProperty false.

I realise that the information might be sort of scarce, but I am looking for what I might have forgotten in this process.

EDIT: We are using CSLA as well and I guess (I'm new at this whole thing) this has something to do with why we want to do it like this.

EDIT (Sollution): As you can see in my answer below, the problem was that I had not set up the CurrentItemChanged event like I should have.

+14  A: 

If you're trying to get them to be read only, then just set the .ReadOnly property to true.

Alternatively, if you're never ever using these textboxes for editing, then maybe just use a Label instead?

EDIT: Ahh it appears this more of a CSLA-framework question than a pure windows forms question. I've never even heard of CSLA before this question, but it looks interesting.

ZaijiaN
I am basically doing what I'm told being a new developer, and my colleagues say all logic is to be placed in the business objects. This is to make it "easy" to change the GUI in the future if we want to do that.
Sakkle
Long story short... this is to be done with CanWriteProperty and no other sollution is acceptable.
Sakkle
We're going to need more specific information about your classes then.
ZaijiaN
I've added a little more info to the original post
Sakkle
It's basically a CSLA thing, yes. It all confuses me a little but the decission has been made so we have to stik to it :P
Sakkle
+1  A: 

i think you mean ReadOnly property

Avram
Unless my more experiensed colleague is on crack... I don't think I do. I will look into it though. However, this is the way I did it on the other usercontrol i just finnished.
Sakkle
I've commented further on this in my origianl post
Sakkle
+3  A: 

If you are databinding to properties of the control just bind the "ReadOnly" property of the textbox to the "CanWrite" property of your business object.

KevB
Do you mean setting the TextBox to read only in the properties window? Bacuse if you do, I can't. My code reviewer would have a seisure... All logic has to be placed in the underlying business object.
Sakkle
No I just mean use the same sort of logic you use for binding to the "Text" property of the TextBox to bind to the "ReadOnly" property. You may need to introduce another property on your business object as "CanWrite" being true would mean that the "ReadOnly" property would get set to true
KevB
Also, if you are using CSLA, you might want to have a look in the CSLA book, I'm pretty sure this sort of stuff is covered in there (its been a while since I've read it)
KevB
Hm... I'll look into this now
Sakkle
+1  A: 

To make this work you need to do the following:

  1. Make sure the TextBox is databound to the right property in the correct way

  2. Set up the needed checks for each textBox in the CanWriteProperty override in your root object

    if (propertyName == OpeningDateProperty.Name) return false;
    
  3. Make sure the rootBindingsource's CurrentItemChanged event is set up right

    private void rootBindingSource_CurrentItemChanged(object sender, EventArgs e)
    {
        readWriteAuthorization1.ResetControlAuthorization();
    }
    
  4. Make sure the texBox's "ApplyAuthorization on ReadWriteAuthorization" is set to true

This solved the problem for me.

Sakkle
I had this all set up right except the CurrentItemChanged
Sakkle