如何设置列表框项目的颜色以匹配我单击的图片框的颜色

时间:2015-05-26 21:52:31

标签: c# listbox

我有一个包含100个项目的列表框和几个不同颜色的图片框。单击图片框时,我希望列表框中当前所选项目的背景更改为图片框的颜色。我想我需要一些方法从picturebox的click事件中调用listbox drawitem事件,但我不知道如何做到这一点。

到目前为止,我有以下代码,但这只是将所选项目设置为默认颜色blue:

    private void redBox_Click(object sender, EventArgs e)
    {
        Color redBoxColour = redBox.BackColor;


    }

    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {

        if (e.Index<0) return;
        //if the item state is selected them change the back color 
        if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            e = new DrawItemEventArgs(e.Graphics, 
                                      e.Font, 
                                      e.Bounds, 
                                      e.Index,
                                      e.State ^ DrawItemState.Selected,
                                      e.ForeColor, 
                                      e.BackColor);

        // Draw the background of the ListBox control for each item.
        e.DrawBackground();
        // Draw the current item text
        e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
        // If the ListBox has focus, draw a focus rectangle around the selected item.
        e.DrawFocusRectangle();


    }

任何帮助都非常感激。

2 个答案:

答案 0 :(得分:0)

请改用ListView。您可以使用ListBox完成ListView所做的一切。

然后只需更改所选索引BackColor,如下所示:

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < listView1.SelectedIndices.Count; i++)
            listView1.Items[listView1.SelectedIndices[i]].BackColor = pictureBox1.BackColor;
    }

你要省去很多麻烦

答案 1 :(得分:0)

您需要创建一个变量来存储所选PictureBox的颜色:

Color selectedColor;

然后,您可以通过Click事件单击PictureBox时设置此变量值(颜色)。所有图片框都应在点击时调用此事件:

    private void picBox_Click(object sender, EventArgs e)
    {
        PictureBox selectedPic = (PictureBox)sender;
        selectedColor = selectedPic.BackColor;
        listBox1.Refresh(); // to update the actual selected item
    }

修改DrawItem函数以使用此颜色,而不是当前使用的e.BackColor:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{

    if (e.Index<0) return;
    //if the item state is selected them change the back color 
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
        e = new DrawItemEventArgs(e.Graphics, 
                                  e.Font, 
                                  e.Bounds, 
                                  e.Index,
                                  e.State ^ DrawItemState.Selected,
                                  e.ForeColor, 
                                  selectedColor);

    // Draw the background of the ListBox control for each item.
    e.DrawBackground();
    // Draw the current item text
    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
    // If the ListBox has focus, draw a focus rectangle around the selected item.
    e.DrawFocusRectangle();
}

在此之前我需要采取一些步骤,我不知道你是否做过:

1)将其添加到表单构造函数:

listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(listBox1_DrawItem);

2)将listBox属性DrawMode更改为OwnerDrawFixed