1

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.

3
  • ASP.Net? Windows Forms? WPF? Commented Feb 18, 2009 at 16:40
  • WinForms... sorry. Editing original post Commented Feb 18, 2009 at 16:42
  • Can you tell us what your objects are and what they are for? For instance, what is the class that the code snippet is from? What is its base class? Are OpeningDateProperty/ChangeDateProperty/CloseDateProperty all TextBoxes? Commented Feb 18, 2009 at 18:26

4 Answers 4

14

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
Long story short... this is to be done with CanWriteProperty and no other sollution is acceptable.
We're going to need more specific information about your classes then.
I've added a little more info to the original post
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
3

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.

4 Comments

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.
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
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)
Hm... I'll look into this now
1

i think you mean ReadOnly property

2 Comments

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.
I've commented further on this in my origianl post
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.