我试图在CheckedListBox控件中绘制或更改项的字符串。因此,我创建了来自CheckedListBox的自定义控件。
public class CheckedListBoxAdv : CheckedListBox
{
public CheckedListBoxAdv()
:base()
{
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
//I want to change the text alone this place. But I cannot access the text part of the item.
}
}有什么办法单独修改文本吗?
发布于 2016-05-27 10:55:09
你不需要任何绘画。可以创建包含Item和Name属性的类,然后重写类的ToString()方法以返回需要在CheckedListBox中显示的内容。
public class Item
{
public int Value { get; set; }
public string Name { get; set; }
public override string ToString()
{
return this.Name;
}
}这样,您就可以用项目填充CheckedListBox。它显示了Name属性,但您也可以访问Value属性:
private void Form1_Load(object sender, EventArgs e)
{
this.checkedListBox1.Items.Clear();
this.checkedListBox1.Items.Add(new Item() { Value = 1, Name = "One" });
this.checkedListBox1.Items.Add(new Item() { Value = 2, Name = "two" });
this.checkedListBox1.Items.Add(new Item() { Value = 3, Name = "three" });
//Change the Name of item at index 1 (2,"two")
((Item)this.checkedListBox1.Items[1]).Name = "Some Text";
//But the value is untouched
MessageBox.Show(((Item)this.checkedListBox1.Items[1]).Value.ToString());
}发布于 2016-05-27 10:35:06
试试this.checkedListBoxName.Items:checkedListBoxName是checkedListBox的名称
例:this.checkedListBoxName.Items[0] = "abc";
https://stackoverflow.com/questions/37480977
复制相似问题