c#winform列表框项目与分离 [英] c# winform listbox item with seperation

查看:64
本文介绍了c#winform列表框项目与分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



低于我的代码列表框绘制项目但没有工作,请检查并更正。


below my code listbox draw item but not worked,please check and correct it.

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
            int index = e.Index;
            Graphics g = e.Graphics;
            Color color;
            if (selected == true)
            {
                color = Color.Red;
            }
            else
            {
                color = Color.Pink;
            }
            /* Draw Background */
            Color borderColor = Color.Black;
            g.DrawRectangle(new Pen(borderColor), e.Bounds);
            g.FillRectangle(new SolidBrush(color), e.Bounds);

            /* Draw Item Text */
            g.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Black), e.Bounds, StringFormat.GenericDefault);

            e.DrawFocusRectangle();
        }

推荐答案

也许你没有在Listbox中设置DrawDode属性为OwnerDrawFixed或OwnerDrawVariable。
Maybe you didn't set in your listbox the DrawMode property to OwnerDrawFixed or OwnerDrawVariable.


这篇关于c#winform列表框项目与分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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