#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?也就是开始如何设置哪几项为选中,选中改变后怎么判断状态发生变化?