Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

ReadOnlyAttribute

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Allen - 19 Feb 2007 15:01 GMT
Hi there,

Given a "PropertyGrid" with one particular property that appears as follows:

-Size
    Width    10
    Height   20

where "Size" is the native .NET "Size" structure, if the property is marked with the "ReadOnlyAttribute" then all three lines above appear grayed out as usual. However, if you replace "Size" with your own customized "Size" structure" instead (not inherited from the native .NET "Size" structure), then "Size" above will now be grayed out but not the "Width" and "Height" lines. Is there no way to propagate the "ReadOnlyAttribute" to all children or do we really have to handle this manually (by creating a read-only "PropertyDescriptor" for all children). Thanks.
John Allen - 19 Feb 2007 15:09 GMT
Sorry, I should clarify that the read-only attribute is applied at runtime (on-the-fly) depending on conditions.
Jon Skeet [C# MVP] - 19 Feb 2007 15:13 GMT
> Sorry, I should clarify that the read-only attribute is applied at
> runtime (on-the-fly) depending on conditions.

How are you applying attributes at runtime? As far as I'm aware,
whether or not an attribute is applied to something is a compile-time
decision, and can't be changed at runtime.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

John Allen - 19 Feb 2007 15:37 GMT
> How are you applying attributes at runtime? As far as I'm aware,
> whether or not an attribute is applied to something is a compile-time
> decision, and can't be changed at runtime.

You can create your own "PropertyDescriptor" objects at runtime and take
complete control over your properties including their attributes (augmenting
or overriding whatever you want). See
"PropertyDescriptor.CreateAttributeCollection()" and
"PropertyDescriptor.AttributesArray" for instance. Note that you can can
take control over all types in general using the
"TypeDescriptionProviderAttribute" (for 2.0 and later),
"ICustomTypeDescriptor" and (to some extent) "TypeConverterAttribute". The
documentation to figure this all out is no picnic however.
John Allen - 19 Feb 2007 15:41 GMT
BTW, "PropertyDescriptor.IsReadOnly", "PropertyDescriptor.IsBrowsable", etc.
complicate things even further (noting that the latter property isn't event
respected by the "PropertyGrid" but this is likely a bug and I've reported
it to MSFT).
Jon Skeet [C# MVP] - 19 Feb 2007 15:43 GMT
> > How are you applying attributes at runtime? As far as I'm aware,
> > whether or not an attribute is applied to something is a compile-time
[quoted text clipped - 9 lines]
> "ICustomTypeDescriptor" and (to some extent) "TypeConverterAttribute". The
> documentation to figure this all out is no picnic however.

Ah, I see - not like the normal way of applying attributes at all.

If your property *starts off* read-only, is it greyed out in the
property grid? I wonder whether it's not noticing the change.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

John Allen - 19 Feb 2007 16:04 GMT
>> > How are you applying attributes at runtime? As far as I'm aware,
>> > whether or not an attribute is applied to something is a compile-time
[quoted text clipped - 13 lines]
>
> Ah, I see - not like the normal way of applying attributes at all.

Yes, and I think the only time you typically have to resort to this is when
you need to get granular control over a "PropertyGrid" at runtime (there is
no other way AFAIK and the techniques are documented by both MSFT and others
of course). Figuring out all the details is a huge pain however (and it
takes an inordinate amount of code to implement ths simplest change). Is it
just me or are the .NET docs in general very weak?

> If your property *starts off* read-only, is it greyed out in the
> property grid? I wonder whether it's not noticing the change.

If I simply apply the read-only attribute to my customized "Size" property
then it grays it out but not its sub-properties ("Width" and "Height"). You
would think that it would but then again, my "Size" property is actually a
member of a larger object which I'm customizing the properties for (using
"PropertyDescriptor" objects). I see no reason why the read-only attribute
shouldn't be propagated to children however (without manual intervention
since again, it takes a lot of code to do this).
Jon Skeet [C# MVP] - 19 Feb 2007 19:01 GMT
> > If your property *starts off* read-only, is it greyed out in the
> > property grid? I wonder whether it's not noticing the change.
[quoted text clipped - 6 lines]
> shouldn't be propagated to children however (without manual intervention
> since again, it takes a lot of code to do this).

Is your Size type definitely a struct and not a class? I wonder if that
would make a difference - because strictly speaking, you could change
the contents of a reference type object without changing which object
was being referred to.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

John Allen - 20 Feb 2007 13:01 GMT
> Is your Size type definitely a struct and not a class? I wonder if that
> would make a difference - because strictly speaking, you could change
> the contents of a reference type object without changing which object
> was being referred to.

Yes, it is a struct. I'm still experimenting but will likely toss in the
towel and manually code things if I can't resolve it soon. Thanks again for
your help.
Jon Skeet [C# MVP] - 20 Feb 2007 13:43 GMT
> > Is your Size type definitely a struct and not a class? I wonder if that
> > would make a difference - because strictly speaking, you could change
[quoted text clipped - 4 lines]
> towel and manually code things if I can't resolve it soon. Thanks again for
> your help.

No problem (not that my help has been anything!) - but it does sound
interesting.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Stoitcho Goutsev (100) - 19 Feb 2007 16:02 GMT
John you need to refresh the property gred after you change the attribute. Try applying RefreshPropertiesAttrubute as well.

Signature

HTH
Stoitcho Goutsev (100)

 Sorry, I should clarify that the read-only attribute is applied at runtime (on-the-fly) depending on conditions.
John Allen - 19 Feb 2007 16:15 GMT
Thanks. I'm already applying it with no effect however. Note that it's not grayed out even if I apply the read-only attribute at compile time (to the "Size" property). Since I'm customizing "PropertyDescriptor" objects at a higher level however, that may be impacting things but I don't know why. It seems counter-intuitive.to me (I think it should work regardless IOW).
 John you need to refresh the property gred after you change the attribute. Try applying RefreshPropertiesAttrubute as well.

 --
 HTH
 Stoitcho Goutsev (100)
   "John Allen" <no_spam@_nospam.com> wrote in message news:OiuprfDVHHA.388@TK2MSFTNGP04.phx.gbl...
   Sorry, I should clarify that the read-only attribute is applied at runtime (on-the-fly) depending on conditions.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.