首页 新闻 会员 周边

UITypeEditor设置属性的时候,使用了同一个对象

0
悬赏园豆:20 [待解决问题]

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 属性里也变更成和 CheckedListBoxCollectionProperty 一样了。看起来 CheckedListBoxUITypeEditor 只生成了一个对象,被两个属性共用,希望两个属性单独使用自己的 CheckedListBoxUITypeEditor 对象,需要怎么修改呢?

无路可逃的主页 无路可逃 | 初学一级 | 园豆:194
提问于:2020-04-22 10:34

我这个问题是没问明白么?咋没人看呢

无路可逃 3年前
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册