如果列表框项目颜色存在于另一个ListBox中,如何更改它

时间:2014-09-24 07:39:13

标签: c# listbox

我要求,它的窗口形成应用程序。我是C#的新手,需要你的帮助才能实现以下目标 SourceListbox和DestListbox以及四个按钮。 如果在复制选定按钮的帮助下将SourceListbox项颜色添加到另一个DestListBox中,如何将其更改为绿色。 再次如果我从DestListbox中删除项目,该项目应该在SourceListbox中转到黑色。 请有人帮帮我。

我无法插入图片以便于理解

请参阅以下链接 http://r4r.co.in/c1/01/tutorial/csharp/ListBox.shtml
完全像上面的例子。将项目添加到DestListBox后,源和目标列表框中匹配的项目应该在两个列表框中都是绿色文本。

以下代码为第一项添加绿色后, 但是选择的下一个项目只会变为绿色,这是我不想要的。

private void button1_Click(object sender,EventArgs e)         {

        foreach (string str in listBox1.SelectedItems)
        {
            listBox2.Items.Add(str);                

            SourceListbox.DrawMode = DrawMode.OwnerDrawVariable;// OwnerDrawFixed;                
           SourceListbox.DrawItem += s_lstbxChannel_DrawItem;                
        }         

    } 



    void s_lstbxChannel_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        bool isItemSelected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
        int itemIndex = e.Index;

        if (itemIndex >= 0 && itemIndex < listBox1.Items.Count)
        {
            Graphics g = e.Graphics;         // Background Color

            SolidBrush backgroundColorBrush = new SolidBrush((isItemSelected) ? Color.White : Color.White);

            g.FillRectangle(backgroundColorBrush, e.Bounds);         // Set text color

            string itemText = listBox1.Items[itemIndex].ToString();

            SolidBrush itemTextColorBrush = (isItemSelected) ? new SolidBrush(Color.Green) : new SolidBrush(Color.Black);

            g.DrawString(itemText, e.Font, itemTextColorBrush, listBox1.GetItemRectangle(itemIndex).Location);// Clean up     

            backgroundColorBrush.Dispose();

            itemTextColorBrush.Dispose();
        }
        e.DrawFocusRectangle();
    }

1 个答案:

答案 0 :(得分:0)

查看Background color of a ListBox item (winforms)

在那里,您可以看到如何更改ListBoxItem的颜色。有了它,你可以做类似的事情:

    public Form1()
    {
        InitializeComponent();
        SourceListbox.DrawMode = DrawMode.OwnerDrawFixed;
        SourceListbox.DrawItem += SourceListbox_DrawItem;
    }

    //global brushes with ordinary/selected colors
    private SolidBrush reportsForegroundBrushSelected = new SolidBrush(Color.White);
    private SolidBrush reportsForegroundBrush = new SolidBrush(Color.Black);
    private SolidBrush reportsBackgroundBrushSelected = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight));
    private SolidBrush reportsBackgroundBrush1 = new SolidBrush(Color.White);
    private SolidBrush reportsBackgroundBrush2 = new SolidBrush(Color.Green);

    //custom method to draw the items, don't forget to set DrawMode of the ListBox to OwnerDrawFixed
    private void SourceListbox_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);

        int index = e.Index;
        if (index >= 0 && index < SourceListbox.Items.Count)
        {
            string text = SourceListbox.Items[index].ToString();
            Graphics g = e.Graphics;

            //background:
            SolidBrush backgroundBrush;
            if (selected)
                backgroundBrush = reportsBackgroundBrushSelected;
            else if (DestListbox.Items.Contains(SourceListbox.Items[index]))
                backgroundBrush = reportsBackgroundBrush2;
            else
                backgroundBrush = reportsBackgroundBrush1;
            g.FillRectangle(backgroundBrush, e.Bounds);

            //text:*/
            SolidBrush foregroundBrush = (selected) ? reportsForegroundBrushSelected : reportsForegroundBrush;
            g.DrawString(text, e.Font, foregroundBrush, SourceListbox.GetItemRectangle(index).Location);
        }

        e.DrawFocusRectangle();
    }

    private void buttonCopySelected_Click(object sender, EventArgs e)
    {
        foreach (int idx in SourceListbox.SelectedIndices)
        {
            DestListbox.Items.Add(SourceListbox.Items[idx]);
        }
    }

从SourceListbox复制到DestListbox的每个项目都变为绿色