2014-09-24 34 views
-2

我有要求,它的窗口窗体应用程序。我是C#的新手,需要您的帮助才能实现 SourceListbox和DestListbox以及四个按钮。 如何通过复制选定按钮的帮助将SourceListbox项目颜色更改为绿色,如果将其添加到另一个DestListBox中。 再次如果我从DestListbox中删除项目,该项目应该在SourceListbox中转到黑色。 有人可以帮我解决。如何更改列表框项目的颜色,如果它存在于另一个列表框中

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

请参考下面 http://r4r.co.in/c1/01/tutorial/csharp/ListBox.shtml
链接酷似上面的例子。项目被添加到DestListBox后,源和目标列表框中匹配的项目应该在两个列表框中均为绿色文本。

下面的代码给第一项绿色, 但选择下一个项目只打算绿色,这是我不希望以后加入。

私人无效的button1_Click(对象发件人,EventArgs的){

 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

,其他用户可以编辑它变成你的问题。您的问题,因为它是非常不清楚..什么是SourceListbox和DestListbox,你使用winforms/wpf/asp.net? – Sayse 2014-09-24 07:42:59

回答

0

看一看Background color of a ListBox item (winforms)

那里你可以看到如何改变ListBoxItems的颜色。有了这样的,你可以这样做:

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每一个项目变成绿色

+0

是否奏效?如果是这样的话:你能否把这个标记为答案? – gottsche 2014-09-25 06:29:45

+0

我已经试过了,不符合我的要求。试图现在更好地解释我的问题,PL看看它 – 2014-09-25 09:59:57

+0

,所以你不希望backgroundcolor设置为绿色,但textcolor?而且你希望它在两个Listbox中,不仅在SourceListbox中?并且在删除SourceListbox中的行之后应该看起来像复制之前那样? – gottsche 2014-09-25 11:07:54

相关问题