更改列表框中所选项目的颜色 [英] changing selected itms color in a listbox

查看:28
本文介绍了更改列表框中所选项目的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从列表框控件中更改所选项目的颜色如何做到这一点在 windows(Winforms) 中

i want to change the color of a selected items from a list box control how to do that in windows(Winforms)

推荐答案

据我所知,如果您想这样做,您需要创建 ListBox.DrawMode OwnerDrawFixed 并将事件处理程序添加到 DrawItem 方法.

As far as I know if you want to do that you need to make the ListBox.DrawMode OwnerDrawFixed and add an event handler on to the DrawItem method.

这样的事情可能会做你想做的事:

Something like this might do what you want:

    private void lstDrawItem(object sender, DrawItemEventArgs e)
    {
        ListBox lst = (ListBox)sender;
        e.DrawBackground();
        e.DrawFocusRectangle();

        DrawItemState st = DrawItemState.Selected ^ DrawItemState.Focus;
        Color col = ((e.State & st) == st) ? Color.Yellow : lst.BackColor;

        e.Graphics.DrawRectangle(new Pen(col), e.Bounds);
        e.Graphics.FillRectangle(new SolidBrush(col), e.Bounds);
        if (e.Index >= 0)
        {
            e.Graphics.DrawString(lst.Items[e.Index].ToString(), e.Font, new SolidBrush(lst.ForeColor), e.Bounds, StringFormat.GenericDefault);
        }
    }

希望有帮助詹姆斯

这篇关于更改列表框中所选项目的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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