Property Attributes c#

S

simonsmith987

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 the Enabled
property:

[Category("Behaviour")]
[Description("Indicates if Button is Enabled or not")]
[BrowsableAttribute(true)]
public bool Enabled

The problem is that I have a few properties with custom types. When
being displayed these combo boxes allow the user to manually type into
the combo box. I do not want this to happen. Is there a property that
restricts the user from typing into the combo box? If not how should I
do this? An example of a property that allows the user to edit the
text in the combo box is below: Any ideas on how to prevent this?

[Browsable(true)]
[TypeConverter(typeof(StateConverter))]
[Description("Should Button be Hidden, Mandatory or Optional")]
public string State
 
N

Nick Hounsome

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 the Enabled
property:

[Category("Behaviour")]
[Description("Indicates if Button is Enabled or not")]
[BrowsableAttribute(true)]
public bool Enabled

The problem is that I have a few properties with custom types. When
being displayed these combo boxes allow the user to manually type into
the combo box. I do not want this to happen. Is there a property that
restricts the user from typing into the combo box? If not how should I
do this? An example of a property that allows the user to edit the
text in the combo box is below: Any ideas on how to prevent this?

[Browsable(true)]
[TypeConverter(typeof(StateConverter))]
[Description("Should Button be Hidden, Mandatory or Optional")]
public string State

I assume that you mean that you are using PropertyGrid.

See ReadonlyAttribute
 
S

simonsmith987

Yes I am using PropertyGrid.

Unfortunately ReadonlyAttribute does not work. It simply restricts all
input to that property. I would like the user to be able to choose
values from the property drop down but not be able to edit these values
manually.
 
S

simonsmith987

Yes I am using PropertyGrid.

Unfortunately ReadonlyAttribute does not work. It simply restricts all
input to that property. I would like the user to be able to choose
values from the property drop down but not be able to edit these values
manually.
 
S

Stoitcho Goutsev \(100\)

Hi,
[Browsable(true)]
[TypeConverter(typeof(StateConverter))]
[Description("Should Button be Hidden, Mandatory or Optional")]
public string State

If you get a combobox for this string property (I see that you have type
converter assigned) that means the type converter supports standard values
that is GetStandardValuesSupported is overridden and returns *true*.

If the combobox is populated that also means you have overridden
GetStandardValues and return collection of standard values.

So far the prop grid shows populated combobox, but the text in the property
grid is editable.

There is one small little step that you need to do in order to make to
behave like the comboboxes for enumerations and bool type. Override
GetStandardValuesExclusive and return *true*. This way the text cannot be
editer and only the values from the list can be used.

My question, though is why you don't use enumeration instead of string if
the possible values (Hidden, Mandatory and Optional) are fixed and
predefined?
 
S

simonsmith987

Thanks Stoitcho, overriding GetStandardValuesExclusive to return true
solved the problem for me. I am not sure why the application is set up
this way as I didn't write it. I'll mention your suggestion to the
other developers. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top