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

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

问题描述

我想覆盖将项目添加到选中列表框中时显示的文本。现在它使用obj.ToString(),但我想附加一些文本,而不改变对象ToString方法。我已经看到了处理ListBoxs的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;

我想做不可能的事吗?

推荐答案

不可能,但难以置信的困难。您建议的方法无效,请注意方法 DrawItem 中类 CheckedListBox 中的元数据:

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天全站免登陆