UITypeEditor использует тот же объект при установке свойств - PullRequest
0 голосов
/ 22 апреля 2020
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;

public class CheckedListBoxEditor
{
    private string _strValue = "(Collection)";
    private string _strValue2 = "(Collection)";

    [Description("This property contains the checked listbox collection.")]
    [EditorAttribute(typeof(CheckedListBoxUITypeEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public string CheckedListBoxCollectionProperty
    {
        get
        {
            return _strValue;
        }
        set
        {
            _strValue = "(Collection)";
        }
    }

    [Description("This property contains the checked listbox collection.")]
    [EditorAttribute(typeof(CheckedListBoxUITypeEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public string CheckedListBoxCollectionProperty2
    {
        get
        {
            return _strValue2;
        }
        set
        {
            _strValue2 = "(Collection)";
        }
    }
}

public class CheckedListBoxUITypeEditor : System.Drawing.Design.UITypeEditor
{
    CheckedListBoxUITypeEditor()
    {
        cbx = new CheckedListBox();
    }

    private CheckedListBox _cbx;

    public CheckedListBox cbx
    {
        [MethodImpl(MethodImplOptions.Synchronized)]
        get
        {
            return _cbx;
        }

        [MethodImpl(MethodImplOptions.Synchronized)]
        set
        {
            if (_cbx != null)
            {
                _cbx.Leave -= bx_Leave;
            }

            _cbx = value;
            if (_cbx != null)
            {
                _cbx.Leave += bx_Leave;
            }
        }
    }

    private IWindowsFormsEditorService es;

    public new override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
    {
        return System.Drawing.Design.UITypeEditorEditStyle.DropDown;
    }

    public new override bool IsDropDownResizable
    {
        get
        {
            return true;
        }
    }

    public new override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
    {
        es = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

        if (es != null)
        {
            LoadListBoxItems();
            cbx.Sorted = true;
            es.DropDownControl(cbx);
        }
        return null;
    }

    private void bx_Leave(object sender, System.EventArgs e)
    {
        My.Settings.UrlsList.Clear();

        {
            var withBlock = cbx;
            for (int i = 0; i <= withBlock.Items.Count - 1; i++)
            {
                string txt = withBlock.Items(i).ToString;
                string chk = withBlock.GetItemChecked(i).ToString;

                string combined = Strings.LCase(txt) + "," + Strings.LCase(chk);
                if (withBlock.Items(i).ToString != "")
                    My.Settings.UrlsList.Add(combined);
            }
        }

        My.Settings.Save();
    }

    private void LoadListBoxItems()
    {
        ArrayList a = new ArrayList();
        foreach (string s in My.Settings.UrlsList)
            a.Add(Strings.Split(s, ","));

        Hashtable h = new Hashtable();

        for (int i = 0; i <= a.Count - 1; i++)
            h.Add((Array)a.Item(i).GetValue(0).ToString, (Array)a.Item(i).GetValue(1).ToString);
        a = null/* TODO Change to default(_) if this is not a reference type */;

        cbx.Items.Clear();

        foreach (DictionaryEntry de in h)
            cbx.Items.Add(de.Key, System.Convert.ToBoolean(de.Value));
        h = null/* TODO Change to default(_) if this is not a reference type */;
    }
}

Как и в приведенном выше коде, свойства CheckedListBoxCollectionProperty и CheckedListBoxCollectionProperty2 доступны отдельно, однако при изменении в свойстве CheckedListBoxCollectionProperty свойство CheckedListBoxCollectionProperty2 также изменяется так же, как и CheckedListBoxCollectionProty. Похоже, что CheckedListBoxUITypeEditor генерирует только один объект, который совместно используется двумя свойствами.

...