如何在ListBoxItems之间绘制一条线

时间:2012-04-07 04:34:50

标签: c# winforms listbox

我希望能够将列表框中的每个项目与水平线分开。

这只是我绘制项目的一些代码。

    private void symptomsList_DrawItem(object sender, System.Windows.Forms.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 */
        g.FillRectangle(new SolidBrush(color), e.Bounds);

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

        e.DrawFocusRectangle();
    }

2 个答案:

答案 0 :(得分:3)

FillRectangle(...) 之后使用:

Color borderColor = Color.Black;
g.DrawRectangle(new Pen(borderColor), e.Bounds);

答案 1 :(得分:1)

///在InitializeComponent()之后添加此行; 症状List.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;

相关问题