C# : change listbox items color

前端 未结 3 540
死守一世寂寞
死守一世寂寞 2020-12-15 07:01

i am working on program on windows forms I have a listbox and I am validating data I want the correct data be added to the listbox with color green while the invalid data ad

相关标签:
3条回答
  • 2020-12-15 07:19

    How about

                MyLB is a listbox
    
                Label ll = new Label();
                ll.Width = MyLB.Width;
                ll.Content = ss;
                if(///<some condition>///)
                    ll.Background = Brushes.LightGreen;
                else
                    ll.Background = Brushes.LightPink;
                MyLB.Items.Add(ll);
    
    0 讨论(0)
  • 2020-12-15 07:23

    Assuming WinForms, this is what I would do:

    Start by making a class to contain the item to add to the listbox.

    public class MyListBoxItem {
        public MyListBoxItem(Color c, string m) { 
            ItemColor = c; 
            Message = m;
        }
        public Color ItemColor { get; set; }
        public string Message { get; set; }
    }
    

    Add items to your listbox using this code:

    listBox1.Items.Add(new MyListBoxItem(Colors.Green, "Validated data successfully"));
    listBox1.Items.Add(new MyListBoxItem(Colors.Red, "Failed to validate data"));
    

    In the properties of the ListBox, set DrawMode to OwnerDrawFixed, and create an event handler for the DrawItem event. This allows you to draw each item however you wish.

    In the DrawItem Event:

    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        MyListBoxItem item = listBox1.Items[e.Index] as MyListBoxItem; // Get the current item and cast it to MyListBoxItem
        if (item != null) 
        {
            e.Graphics.DrawString( // Draw the appropriate text in the ListBox
                item.Message, // The message linked to the item
                listBox1.Font, // Take the font from the listbox
                new SolidBrush(item.ItemColor), // Set the color 
                0, // X pixel coordinate
                e.Index * listBox1.ItemHeight // Y pixel coordinate.  Multiply the index by the ItemHeight defined in the listbox.
            );
        }
        else 
        {
             // The item isn't a MyListBoxItem, do something about it
        }
    }
    

    There are a few limitations - the main one being that you'd need to write your own click handler and redraw appropriate items to make them appear selected, since Windows won't do that in OwnerDraw mode. However, if this is just intended to be a log of things happening in your application, you may not care about items appearing selectable.

    To scroll to the last item try

    listBox1.TopIndex = listBox1.Items.Count - 1;
    
    0 讨论(0)
  • 2020-12-15 07:33

    Not really an answer to your question however you might want to look at ObjectListView. It is a ListView rather than a listbox but it is very flexible and easy to use. It could be used with a single column to represent your data

    I use it to color the status of each line

    http://objectlistview.sourceforge.net/cs/index.html

    This is for WinForms of course.

    0 讨论(0)
提交回复
热议问题