c#propertygrid 自定义属性checklistbox,valuechanged如何获取checklistbox状态?

qq_40934320 2020-12-15 04:14:27
#region propertygrid设置开始
public static ArrayList arraylist = new ArrayList();//输入字段


public class CheckedListBoxUC : CheckedListBox
{
private System.Windows.Forms.Design.IWindowsFormsEditorService m_iws;

private string m_selectStr = string.Empty;

/// <summary>
/// 获取选择的字段,多个字段用"|"隔开
/// </summary>
public string SelectedFields
{
get
{
return this.m_selectStr;
}

}

public void SetCheck(object value)
{
try
{
string s = value.ToString();
string[] result = s.Split('|');

this.Items.Clear();
for (int i = 0; i < arraylist.Count; i++)
{
this.Items.Add(arraylist[i]);
}

for (int i = 0; i < result.Length; i++)
{
for (int k = 0; k < arraylist.Count; k++)
{
if (result[i] == arraylist[k].ToString())
{
//SetItemCheckState(k, CheckState.Checked);
this.SetItemChecked(k, true);
}
}
}
}

catch (Exception ex)
{
//MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

//value = "X|Y";
public CheckedListBoxUC(System.Windows.Forms.Design.IWindowsFormsEditorService iws)
{
this.m_iws = iws;
this.Visible = true;
this.Height = 100;
this.BorderStyle = BorderStyle.None;
//添加事件
this.Leave += new EventHandler(checkedListBoxUC_Leave);
this.SelectedIndexChanged += new System.EventHandler(this.checklist_SelectedIndexChanged);
try
{
/*
string[] strsFields = LayerParams.GetFields();
this.BeginUpdate();
this.Items.Clear();
if (null != strsFields)
{
for (int i = 0; i < strsFields.Length; i++)
{
this.Items.Add(strsFields[i]);
}
}
*/
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
this.EndUpdate();
}
}

private void checklist_SelectedIndexChanged(object sender, EventArgs e)
{
// m_selectStr = this.SelectedItem.ToString();
}

void checkedListBoxUC_Leave(object sender, EventArgs e)
{
List<string> lstStrs = new List<string>();
for (int i = 0; i < this.Items.Count; i++)
{
if (this.GetItemChecked(i))
{
lstStrs.Add((string)this.Items[i].ToString());
}

}
m_selectStr = string.Join("|", lstStrs.ToArray());
this.m_iws.CloseDropDown();
}
} //checklistbox

public class ListBoxUCConverter : System.Drawing.Design.UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return System.Drawing.Design.UITypeEditorEditStyle.DropDown;
}

public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
System.Windows.Forms.Design.IWindowsFormsEditorService iws = (System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));
if (iws != null)
{
CheckedListBoxUC chkListBoxUC = new CheckedListBoxUC(iws);
//chkListBoxUC.DataSource = arraylist;
chkListBoxUC.SetCheck(value);
iws.DropDownControl(chkListBoxUC);

return chkListBoxUC.SelectedFields;
}
return value;
}
} //convertor


后面如何获取checklistbox状态,及如何调用checklistbox?也就是开始如何设置哪几项为选中,选中改变后怎么判断状态发生变化?
...全文
100 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,561

社区成员

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

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

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