2

I'd like to use the functionality of a ComboBox as edit option for a var in the properties window of a custom control / component. Not the ComboBox component itself.

As example:

private string[] _stringArray = { "string0", "string1" };
public string[] StringArray
{
    get { return _stringArray; }
    //callback
    //set { _stringArray = value; }
}

As you know this will give me the object browser as view/edit option in the property window. Funny thing that I can edit the values even with no setter.

property

In my researches I found out that it is possible ("UITypeEditorEditStyle.DropDown"). But I have no idea how to implement that. Or what [Instructions] I could set for the "StringArray".

My final goal is a copy of the object selector drop-down of visual studio as a property parameter: goal

With custom event handling of course. But as you see I'm far away to realize that. :(

I have been looking for a tutorial on the following topics for a long time:

  • [Designer] instructions reference
  • A basic tutorial how to manage the display style of properties

However I'm tired of my unsuccessful researches. Some good links are always welcome.

  • 1
    You can get started with https://stackoverflow.com/q/5171037/1070452 among several dozen detailed answers here; there are also several tutorials and authoritative articles at MSDN/MS Docs. A string array is an odd choice, but if it is not 'settable' consonsider an enum in which case a TypeConverter will work. – Ňɏssa Pøngjǣrdenlarp Oct 26 '20 at 00:09
  • string[] is just the default type for a ComboBox Lines, that's why I chose that type as example... – Luckylazuli Oct 26 '20 at 00:26
  • 1
    ComboBox uses an ObjectCollection for the Items Collection - you can store almost anything in it, not a string array. It is more than just a "display style" you are after. Some will use a custom UITypeEditor but that is just for the property pane at design time. Some will use the `StandardValues` feature of a `TypeConverter` which provides the UI with a list for the prop pane. Details for that are also in abundance here and at MS Docs. – Ňɏssa Pøngjǣrdenlarp Oct 26 '20 at 00:40
  • Got it. Maybe my problem is the "overflow" of information. ;) Well with enums I get a dd by default, ok. I think your link is nice. Idk why I don't find such a page by myself. Thanks. I'll go trough the TypeConverter thing. It's just.. ahh can't be that difficult. But it is (for me). – Luckylazuli Oct 26 '20 at 01:06

1 Answers1

2

UPDATE:

After I more or less understood the principle (from the link in the comments, thanks) I came to an interim solution. I realized that I need at least an int var to set a selected `index`. I thought / hoped that VS can do this automatically. Like it does with enums. And my lack of knowledge concerning [Instructions].

I could define a string variable as a placeholder for the selected index of the array in order to do without the TypeConverter, but that would make even less sense. I really don't need another abstract variable for nothing.

So the basis drop-down, which e.g. can display enums directly, does not appear to be applicable. So they use a trick with "UITypeEditorEditStyle.DropDown", which actually isn't a drop-down. It's just a button where you can place the control of your choice. In my case a ListView. Since the "drop" of the "down" already exists. Looks like cheating. ;)

//...
 
[TypeConverter(typeof(StringArrayConverter))]
public interface IStringArray
{
    int SelectedIndex { get; set; }
    string[] StringArray { get; set; }
}

public class DropDownStringArray : IStringArray
{
    private string[] _stringArray = { "string0", "string1", "string2", "string3", "string4", "string5", "string6" };
    public int SelectedIndex { get; set; }
    public string[] StringArray
    {
        get { return _stringArray; }
        set { _stringArray = value; }
    }
}

private DropDownStringArray _ddsa = new DropDownStringArray();
[Editor(typeof(StringArrayTypeEditor), typeof(UITypeEditor))]
public DropDownStringArray MyDropDownStringArray
{
    get { return _ddsa; }
    set { _ddsa = value; }
}
 
//...
 
public class StringArrayConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return destinationType == typeof(string);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            var sa = value as IStringArray;
            if (sa != null) { return sa.StringArray[sa.SelectedIndex]; }
        }
        return "(none)";
    }
}

public class StringArrayTypeEditor : UITypeEditor
{
    private IWindowsFormsEditorService _editorService;

    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.DropDown; }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        _editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
        DropDownStringArray ddsa = (DropDownStringArray)value;
        ListBox lb = new ListBox();
        lb.SelectionMode = SelectionMode.One;
        for (int i = 0; i < ddsa.StringArray.Length; i++) { lb.Items.Add(ddsa.StringArray[i]); }
        lb.SetSelected(ddsa.SelectedIndex, true);
        lb.SelectedValueChanged += OnListBoxSelectedValueChanged;
        _editorService.DropDownControl(lb);
        if (lb.SelectedItem != null) { ddsa.SelectedIndex = lb.SelectedIndex; }
        return value;
    }

    private void OnListBoxSelectedValueChanged(object sender, EventArgs e)
    {
        _editorService.CloseDropDown();
    }
}
 

result

Which actually copy the entire class just to change the SelectedIndex. The right thing would be to abuse the SelectedIndex and convert it to a string or something like that. I think I do not care about that anymore. Rather to catch some fresh air. ;)

Maybe that will help someone else.

Note: This is not a practical propose. As example SelectedIndex will not be updated if you change the (length) of the array. I've choosen string[] because it's a really basic and well known type. I am aware that my "program" has no real use. It was just about understanding the principle.

  • Your code works. As I am currently in need of a property in my custom component that can offer a choice like a combobox I am trying to use this code. One problem I still face is how to know when a value was choosen. In other words, someone chooses a value from the list, for example string3` then how can i get notified of this ? – GuidoG Jan 21 '21 at 14:28