Takkun
Takkun

Reputation: 6361

How do I draw a line between ListBoxItems

I want to be able to separate each item in a listbox with a horizontal line.

This is just some of my code for drawing the items.

    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();
    }

Upvotes: 2

Views: 2888

Answers (2)

Compee
Compee

Reputation: 11

//add this line after InitializeComponent(); symptomsList.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;

Upvotes: 0

Vano Maisuradze
Vano Maisuradze

Reputation: 5899

After FillRectangle(...) use this:

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

Upvotes: 3

Related Questions