1

I am working on a custom component, and now I need to add a public property that is visible in properties windows of VS.
This property must be a list of values where the user can choose from. Like a combobox.
I googled around a lot and found this question on SO and tried it, but it seems a lot of work so I wanted a more elegant way. Also, with this code I have no notification when a user chooses an item from the list.
So I found that the proper way of showing a list a user can choose from (on a custom component's properties in the property window of VS) is like the code below

public enum BUTTONSTYLE { Style_Solid, Style_GradientA, Style_GradientB, Style_Image, Style_Liquid };
BUTTONSTYLE _buttonStyle;

[DefaultValue(BUTTONSTYLE.Style_Solid)]
[Category("Appearance")]
[Browsable(true)]
[Description("Sets style of the button")]
public BUTTONSTYLE newbuttonstyle
{
    get
    {
        return _buttonStyle;
    }
    set
    {
        _buttonStyle = value;
    }
}

While this works very well, my problem is that this needs a enum that is defined and cannot change. In my case the values in BUTTONSTYLE can change. But it's not possible to change the items in an enum. Allthough this answer says it can be done, I tried it and it did not work.

So I am thinking that probably I am doing this a complete wrong way.
How can I add such kind of property to my custom control the proper way ?

For clarity, the property is not a combobox, but should let the user pick a value like if it was a combobox

enter image description here

EDIT

for clarity I will try to explain in other words what my problem is.
I need a property that will present a list to the user, so he can pick an item from this list.
What I found so far is that I should use an enum for this, that will show up in the propertie window as a list (see also the image) where the user can pick from.
But, this is not usefull for me because when I use an enum than the items in the list are defined at designtime and cannot change.
In my case, the items that are presented to the user could change, based on the values of other properties.
So I cannot use an enum after all, so what is the proper way to do this ?

I hope my problem is a bit more clear now

GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • At which moment you want to change the values? I can't follow you: "this needs a enum that is defined and cannot change". – Rekshino Jan 21 '21 at 16:14
  • You should describe a bit better what can change, how and when. Do you mean *in general* and not related to this specific context (I assume you cannot add more rendering methods at run-time. Maybe remove some...)? As you usually do with a custom TypeConverter + Attributes? – Jimi Jan 21 '21 at 16:40
  • The basic idea is creating a new `TypeConverter` and returning a list of predefined values by overriding [`GetStandardValues`](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.typeconverter.getstandardvalues?view=net-5.0&WT.mc_id=DT-MVP-5003235). For example you can even [load dropdown values dynamically](https://stackoverflow.com/a/53891285/3110834) from a database. – Reza Aghaei Jan 21 '21 at 18:58
  • @Rekshino The values can change based on the values of other properties. The values in the dropdown list is not fixed, they can change. – GuidoG Jan 21 '21 at 20:42
  • @Jimi I don't understand your question. What I need is a property on my custom component, where the user can choose from a list, but the list could change based on the values of other properties. The code I have now does presents the user with such a list, but since it is an enum I cannot change the values in the list – GuidoG Jan 21 '21 at 20:44
  • @Jimi Yes its in general, the list in my question is just an example. In my case it will be a list of table names, and this list could change based on the values of other properties. An enum works nice to show the user a list he can choose from, but I cannot change the items in the enum. – GuidoG Jan 21 '21 at 20:46
  • @Rekshino When I say "this needs an enum that is defined and cannot change" i mean that i need another method of showing a list to the user where he can pick from. What I found so far is that you need to use an enum to make the property work with a list. But I cannot alter the items in the enum. I need something else then an enum so I can alter the items and it will still present the user with a list to choose from – GuidoG Jan 21 '21 at 20:49
  • @RezaAghaei The SO link in your comment looks promising. Thank you for that – GuidoG Jan 21 '21 at 20:59
  • @Jimi So TypeConverter and Attributes are the keywords I need to search for. Thank you this could point me into the correct direction – GuidoG Jan 21 '21 at 21:00
  • Reading the comments here, I believe a type converter like what I have created in linked post is the way to go. – Reza Aghaei Jan 21 '21 at 21:05
  • @RezaAghaei Yes it looks like that. Its hard to find what you need when you dont know the correct keywords to look for. So thank you once again for pushing me in the correct direction – GuidoG Jan 21 '21 at 21:07
  • No problem :) That's the point of asking questions here, sharing your attempts and explaining the requirements well. – Reza Aghaei Jan 21 '21 at 21:08
  • Yes, that's what you usually do. I see @Reza Aghaei already gave you a link. Property Attributes can be used to specify default option or default filters; if you need something more dynamic, you can create properties on-the-fly. As Reza mentioned, it's important that you provide clear context, since the use-case usually matters. If you have a data source that provides the properties, then say that explicitly, an enumerator and what it looks like it's applied to, as in the sample code you posted, is *relatively unrelated* to the real matter: you may get *unrelated* suggestions or code samples – Jimi Jan 21 '21 at 21:16
  • @Jimi Yes I understand, but like I also said to Reza, it is hard to look for what you need if you don't know the correct keywords. I only found enum to come close to what I need, so I used it to try to explain my needs. I still have to lookup and try the link from Reza but at least now I know what to look for. – GuidoG Jan 22 '21 at 06:41
  • @RezaAghaei I have tried the code in your link but have some questions about it. So I added a new question here. Since you where the author of the answer I used as a guide, mayby you can help me with this ? [new question](https://stackoverflow.com/questions/65842530/how-to-change-the-items-of-a-dropdown-property-using-a-typeconverter) – GuidoG Jan 22 '21 at 09:33
  • Sure, I'll take a look and share my idea, if I have anything to share. – Reza Aghaei Jan 22 '21 at 09:45

0 Answers0