我可以将 DrawItem 事件处理程序与 CheckedListBox 一起使用吗? [英] Can I use a DrawItem event handler with a CheckedListBox?

查看:11
本文介绍了我可以将 DrawItem 事件处理程序与 CheckedListBox 一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个项目被添加到一个选中的列表框时,我想覆盖显示的文本.现在它正在使用 obj.ToString(),但我想附加一些文本,而不更改对象的 ToString 方法.我已经看到处理 ListBox 的 DrawItem 事件的示例,但是当我尝试实现它们时,我的事件处理程序没有被调用.我注意到 Winforms 设计器似乎不允许我为 DrawItem 事件分配处理程序.固执的我自己加了代码

I would like to override the text displayed when an item is added to a checked list box. Right now it is using obj.ToString(), but I want to append some text, without changing the objects ToString method. I have seen examples of handling the DrawItem event for ListBoxs, but when I try to implement them, my event handler is not called. I have noted that the Winforms designer does not seem to allow me to assign a handler for the DrawItem event. Being stubborn, I just added the code myself

        listbox1.DrawMode = DrawMode.OwnerDrawVariable;
        listbox1.DrawItem += listbox1_DrawItem;

我是在尝试做不可能的事吗?

Am I trying to do the impossible?

推荐答案

并非不可能,但非常困难.你的建议是行不通的,注意 CheckedListBox 类中的元数据,用于方法 DrawItem:

Not impossible, but incredibly difficult. What you suggest will not work, note the meta-data in class CheckedListBox for method DrawItem:

// Summary:
//     Occurs when a visual aspect of an owner-drawn System.Windows.Forms.CheckedListBox
//     changes. This event is not relevant to this class.
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public event DrawItemEventHandler DrawItem;

因此,您唯一的选择是从 CheckedListBox 派生您自己的类,在我有限的测试中,这将是一条漫长的道路.您可以简单地处理绘图,如下所示:

Therefore, your only option is to derive your own class from CheckedListBox, and in my limited testing, this will be a long road. You can handle the drawing simply enough, as such:

public class CustomCheckedListBox : CheckedListBox
{
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        String s = Items[e.Index].ToString();
        s += "APPEND";  //do what you like to the text
        CheckBoxState state = GetCheckBoxState(e.State);  // <---problem
        Size glyphSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, state);
        CheckBoxRenderer.DrawCheckBox(
            e.Graphics, 
            e.Bounds.Location, 
            new Rectangle(
                new Point(e.Bounds.X + glyphSize.Width, e.Bounds.Y), 
                new Size(e.Bounds.Width - glyphSize.Width, e.Bounds.Height)), 
            s, 
            this.Font, 
            false,
            state);
    }
}

注意方法 GetCheckBoxState().你在 DrawItemEventArgs 中得到的是一个 DrawItemState,而不是你需要的 CheckBoxState,所以你必须翻译,这就是事情开始的地方对我来说下坡.

Note the method GetCheckBoxState(). What you get in the DrawItemEventArgs is a DrawItemState, not the CheckBoxState you need, so you have to translate, and that's where things started to go downhill for me.

士兵们,如果你愿意,这应该会让你开始.但我认为这将是一条混乱而漫长的道路.

Soldier on, if you like, this should get you started. But I think it'll be a messy, long road.

这篇关于我可以将 DrawItem 事件处理程序与 CheckedListBox 一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆