• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

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?也就是开始如何设置哪几项为选中,选中改变后怎么判断状态发生变化?
...全文
5 点赞 收藏 回复
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复

还没有回复,快来抢沙发~

发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2020-12-15 04:14
社区公告

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