首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在WinForms .Net 3.5中的CheckedListBox中没有DisplayMember

在WinForms .Net 3.5中的CheckedListBox中没有DisplayMember的问题可以通过以下方法解决:

  1. 使用ListControl.GetItemText方法自定义显示文本。
代码语言:csharp
复制
public class CustomCheckedListBox : CheckedListBox
{
    public CustomCheckedListBox()
    {
        this.ItemCheck += CustomCheckedListBox_ItemCheck;
    }

    private void CustomCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        e.NewValue = e.CurrentValue;
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        if (e.Index >= 0 && e.Index< Items.Count)
        {
            object item = Items[e.Index];
            string itemText = GetItemText(item);

            e.DrawBackground();
            using (SolidBrush brush = new SolidBrush(e.ForeColor))
            {
                e.Graphics.DrawString(itemText, e.Font, brush, e.Bounds);
            }

            if ((e.State & DrawItemState.Focus) == DrawItemState.Focus)
            {
                e.DrawFocusRectangle();
            }
        }
        else
        {
            base.OnDrawItem(e);
        }
    }
}
  1. 使用ListControl.Format事件自定义显示文本。
代码语言:csharp
复制
public class CustomCheckedListBox : CheckedListBox
{
    public CustomCheckedListBox()
    {
        this.ItemCheck += CustomCheckedListBox_ItemCheck;
        this.Format += CustomCheckedListBox_Format;
    }

    private void CustomCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        e.NewValue = e.CurrentValue;
    }

    private void CustomCheckedListBox_Format(object sender, ListControlConvertEventArgs e)
    {
        e.Value = GetItemText(e.ListItem);
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        if (e.Index >= 0 && e.Index< Items.Count)
        {
            object item = Items[e.Index];
            string itemText = GetItemText(item);

            e.DrawBackground();
            using (SolidBrush brush = new SolidBrush(e.ForeColor))
            {
                e.Graphics.DrawString(itemText, e.Font, brush, e.Bounds);
            }

            if ((e.State & DrawItemState.Focus) == DrawItemState.Focus)
            {
                e.DrawFocusRectangle();
            }
        }
        else
        {
            base.OnDrawItem(e);
        }
    }
}

这两种方法都可以实现在WinForms .Net 3.5中的CheckedListBox中自定义显示文本。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

2分11秒

2038年MySQL timestamp时间戳溢出

13分17秒

002-JDK动态代理-代理的特点

15分4秒

004-JDK动态代理-静态代理接口和目标类创建

9分38秒

006-JDK动态代理-静态优缺点

10分50秒

008-JDK动态代理-复习动态代理

15分57秒

010-JDK动态代理-回顾Method

13分13秒

012-JDK动态代理-反射包Proxy类

17分3秒

014-JDK动态代理-jdk动态代理执行流程

6分26秒

016-JDK动态代理-增强功能例子

10分20秒

001-JDK动态代理-日常生活中代理例子

11分39秒

003-JDK动态代理-静态代理实现步骤

8分35秒

005-JDK动态代理-静态代理中创建代理类

领券