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

李察德-泰森 2020-04-21 06:15:48

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 对象,需要怎么修改呢?
...全文
174 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
李察德-泰森 2020-04-23
  • 打赏
  • 举报
回复
李察德-泰森 2020-04-22
  • 打赏
  • 举报
回复
额,是我问的问题不清楚么?呼叫大婶
李察德-泰森 2020-04-22
  • 打赏
  • 举报
回复
自己顶一顶,顶一顶

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧