yonan2236
yonan2236

Reputation: 13649

How to incorporate ListBox in PropertyGridControl?

I saw this in an application and just wondering if this can be done in winforms using C#. I have no experience in customizing PropertyGrid control and I would highly appreciate if you can kindly help me on the codes. Thanks in advance.

Sceenshot:

enter image description here

Upvotes: 1

Views: 603

Answers (1)

virious
virious

Reputation: 571

There are few steps to accomplish that.

1. Define enum for your values.

public enum TypeCodes
{
    Baking,
    Barbecuing,
    Blanching,
    Blind_Baking,
    Boiling,
    Broiling,
    Deep_Frying,
}

2. Create class with property of type List.

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;

namespace PropertyGrid_ListBox_Test
{
    class TestCase
    {
        public TestCase()
        {
            Manufacturer = new List<TypeCodes>();
        }

        [DisplayName("Manufacturer Preparation Type Code")]
        public List<TypeCodes> Manufacturer { get; set; }
    }
}

3. Create UserControl - TypeCodeSelectionControl which will be used to edit this property.

UserControl to edit property value

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace PropertyGrid_ListBox_Test
{
    public partial class TypeCodeSelectionControl : UserControl
    {
        public List<TypeCodes> List { get; set; }
        private IWindowsFormsEditorService editorService;

        public TypeCodeSelectionControl()
        {
            InitializeComponent();
        }

        private void PrepareListBoxes()
        {
            foreach (string myType in Enum.GetNames(typeof(TypeCodes)))
                listBox1.Items.Add(myType);

            foreach (TypeCodes type_code in this.List)
            {
                string enum_value = Enum.GetName(typeof(TypeCodes), type_code);
                listBox1.Items.Remove(enum_value);
                listBox2.Items.Add(enum_value);
            }
        }

        public TypeCodeSelectionControl(List<TypeCodes> list, IWindowsFormsEditorService editorService)
        {
            InitializeComponent();

            this.List = list;
            this.editorService = editorService;

            PrepareListBoxes();
        }

        private void buttonALL_Click(object sender, EventArgs e)
        {
            //
            // Add all items.
            //
            foreach (object obj in listBox1.Items)
            {
                listBox2.Items.Add(obj);
            }
            listBox1.Items.Clear();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            //
            // Add selected item.
            //
            string item_to_add = (string)listBox1.SelectedItem;
            listBox1.Items.Remove(item_to_add);
            listBox2.Items.Add(item_to_add);
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            //
            // Remove selected item.
            //
            string item_to_remove = (string)listBox2.SelectedItem;
            listBox2.Items.Remove(item_to_remove);
            listBox1.Items.Add(item_to_remove);
        }

        private void buttonNone_Click(object sender, EventArgs e)
        {
            //
            // Remove all items.
            //
            foreach (object obj in listBox2.Items)
            {
                listBox1.Items.Add(obj);
            }
            listBox2.Items.Clear();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //
            // Enable "Add" button only if any of items is selected in the left listbox.
            //
            buttonAdd.Enabled = listBox1.SelectedIndex != -1;
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //
            // Enable "Remove" button only if any of items is selected in the right listbox.
            //
            buttonRemove.Enabled = listBox2.SelectedIndex != -1;
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);

            //
            // This occures when dropdown UI editor is hiding.
            //
            if (this.Visible == false)
            {
                //
                // Clear previously selected items.
                //
                if (this.List == null)
                {
                    this.List = new List<TypeCodes>();
                }
                else
                {
                    this.List.Clear();
                }

                //
                // Fill the list with currently selected items.
                //
                foreach (string obj in listBox2.Items)
                {
                    this.List.Add((TypeCodes)Enum.Parse(typeof(TypeCodes), obj));
                }
                editorService.CloseDropDown();
            }
        }
    }
}

4. Create UITypeEditor - TypeCodeEditor which will handle previously created UserControl.

using System;
using System.Collections.Generic;
using System.Drawing.Design;
using System.Windows.Forms.Design;

namespace PropertyGrid_ListBox_Test
{
    class TypeCodeEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;
            if (provider != null)
            {
                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (editorService != null)
            {
                TypeCodeSelectionControl selectionControl = new TypeCodeSelectionControl((List<TypeCodes>)value, editorService);
                editorService.DropDownControl(selectionControl);

                value = selectionControl.List;
            }

            return value;
        }
    }
}

5. Create TypeConverter to display our property value correctly.

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace PropertyGrid_ListBox_Test
{
    class TypeCodeTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                List<TypeCodes> list = new List<TypeCodes>();
                string[] values = ((string)value).Split(new char[] { ';' });
                foreach (string v in values)
                {
                    list.Add((TypeCodes)Enum.Parse(typeof(TypeCodes), v));
                }

                return list;
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                List<TypeCodes> list = (List<TypeCodes>)value;
                string result = "";
                foreach (TypeCodes type_code in list)
                {
                    result += Enum.GetName(typeof(TypeCodes), type_code) + "; ";
                }
                return result;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}

6. Add appropriate attributes to the property so it will become editable with our drop-down custom editor.

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;

namespace PropertyGrid_ListBox_Test
{
    class TestCase
    {
        public TestCase()
        {
            Manufacturer = new List<TypeCodes>();
        }

        [DisplayName("Manufacturer Preparation Type Code")]
        [Editor(typeof(TypeCodeEditor), typeof(UITypeEditor))]
        [TypeConverter(typeof(TypeCodeTypeConverter))]
        public List<TypeCodes> Manufacturer { get; set; }
    }
}

7. Enjoy the results :). enter image description here

I hope that piece of code will help you continue with writing your own custom editors.

Full example is available here: https://github.com/virious/PropertyGrid_CustomDropDownEditor.

Upvotes: 2

Related Questions