473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

ReadOnlyAttribute

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.
Feb 19 '07 #1
11 3389
Sorry, I should clarify that the read-only attribute is applied at runtime (on-the-fly) depending on conditions.
Feb 19 '07 #2
John Allen <no_spam@_nospam.comwrote:
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.

--
Jon Skeet - <sk***@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
Feb 19 '07 #3
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.
Feb 19 '07 #4
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).
Feb 19 '07 #5
John Allen <no_spam@_nospam.comwrote:
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.
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.

--
Jon Skeet - <sk***@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
Feb 19 '07 #6
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.comwrote in message news:Oi*************@TK2MSFTNGP04.phx.gbl...
Sorry, I should clarify that the read-only attribute is applied at runtime (on-the-fly) depending on conditions.
Feb 19 '07 #7

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
John Allen <no_spam@_nospam.comwrote:
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.

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).
Feb 19 '07 #8
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).
"Stoitcho Goutsev (100)" <10*@100.comwrote in message news:%2***************@TK2MSFTNGP02.phx.gbl...
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.comwrote in message news:Oi*************@TK2MSFTNGP04.phx.gbl...
Sorry, I should clarify that the read-only attribute is applied at runtime (on-the-fly) depending on conditions.
Feb 19 '07 #9
John Allen <no_spam@_nospam.comwrote:
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).
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.

--
Jon Skeet - <sk***@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
Feb 19 '07 #10
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.
Feb 20 '07 #11
John Allen <no_spam@_nospam.comwrote:
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.
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.

--
Jon Skeet - <sk***@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
Feb 20 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rein Petersen | last post by:
Hi Folks! Here's a strange behaviour: Without a properties SET accessor (see code below), the property will not serialize. public class myObject {
7
by: Polo | last post by:
Hi All, I would like to apply dynamically the ReadOnlyAttribute(True - False) on a class to lock or not the edition of instances of it in the property grid An other solution is welcome. ...
9
by: James Geurts | last post by:
Hey all... I posted this in the vs.net ide group too, but people are not answering, so I figured that it might be more appropriate here. I'm not sure if I'm adding a designer to my code properly. ...
0
by: micro_bug | last post by:
Now I am developing a tool, just like VS.net Form Edition environment. User can edit a component's properties by a PropertyGrid control. Now I have some questions about this PropertyGrid control:...
0
by: Vladimir.Sakharuk | last post by:
I have created ExpandableObjectConverter in C#. for use in System.Windows.Forms.PropertyGrid. The property grid shows the DiffItem is a object that holds member TheItem. without expanding...
3
by: Tom | last post by:
I'm using the PropertyGrid, and have it bound to a class object. Works fine - however, I have some properties that I want to show up on the grid (i.e. they are browsable) -YET- I don't want the...
5
by: simonsmith987 | last post by:
I am displaying a list of properties on my GUI. All the properties have combo boxes that shouldn't be allowed to be edited. At the minute this works fine for bool values, see the code below for...
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
1
by: srinivas2009123 | last post by:
Hi, I have a class with two Properties like HasSubMenu, and SubMenu , object of this class is bound to property grid. The class is given below. class test { private bool m_HasSubMenu;...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.