首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在CheckedBoxList控件中对项绘制字符串?

如何在CheckedBoxList控件中对项绘制字符串?
EN

Stack Overflow用户
提问于 2016-05-27 10:23:24
回答 2查看 48关注 0票数 1

我试图在CheckedListBox控件中绘制或更改项的字符串。因此,我创建了来自CheckedListBox的自定义控件。

代码语言:javascript
复制
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.

    }        
}

有什么办法单独修改文本吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-27 10:55:09

你不需要任何绘画。可以创建包含ItemName属性的类,然后重写类的ToString()方法以返回需要在CheckedListBox中显示的内容。

代码语言:javascript
复制
public class Item
{
    public int Value { get; set; }
    public string Name { get; set; }
    public override string ToString()
    {
        return this.Name;
    }
}

这样,您就可以用项目填充CheckedListBox。它显示了Name属性,但您也可以访问Value属性:

代码语言:javascript
复制
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());

}
票数 2
EN

Stack Overflow用户

发布于 2016-05-27 10:35:06

试试this.checkedListBoxName.Items:checkedListBoxName是checkedListBox的名称

例:this.checkedListBoxName.Items[0] = "abc";

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37480977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档