none
Change ListBox each item color RRS feed

  • Question

  • Hi,
    In my winform have 2 listbox, selecting items from listbox1 to listbox2, default listbox2 had few items, those color should be in gray color, if we add new item to listbox2 those color should be black,if we change listbox2 item order(Up/Down) the default items color should be gray only.
    Thanks & Regards,
    Bhanu.Ch
    Wednesday, February 9, 2011 5:24 AM

Answers

  • Hi bhanu ch,

    You have to take responsibility for drawing the list elements at an underlying level.

    Declare you will do this:

    listBox.DrawMode = DrawMode.OwnerDrawFixed;
    listBox.DrawItem += new DrawItemEventHandler(listBox_DrawItem);
    
    Then create the event handler:
    
    private void listBox_DrawItem(object sender, DrawItemEventArgs e)
    {
      // Draw the background of the ListBox control for each item.
      e.DrawBackground();
      Brush myBrush = Brushes.Gray; //or whatever...
      // Draw the current item text based on the current 
      // Font and the custom brush settings.
      //
      e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
            e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
        // If the ListBox has focus, draw a focus rectangle 
        // around the selected item.
        //
          e.DrawFocusRectangle();
    }
    
    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Sunday, February 13, 2011 5:27 AM
    Moderator

All replies

  • Do you have a Draw event for your control? Also, I highly recommend you try your scenario in WPF. Then you'll be able to customize everything about the listbox.
    Cornel Croitoriu - Senior Software Developer - www.Biz-Forward.com
    Wednesday, February 9, 2011 1:57 PM
  • Hi bhanu ch,

    You have to take responsibility for drawing the list elements at an underlying level.

    Declare you will do this:

    listBox.DrawMode = DrawMode.OwnerDrawFixed;
    listBox.DrawItem += new DrawItemEventHandler(listBox_DrawItem);
    
    Then create the event handler:
    
    private void listBox_DrawItem(object sender, DrawItemEventArgs e)
    {
      // Draw the background of the ListBox control for each item.
      e.DrawBackground();
      Brush myBrush = Brushes.Gray; //or whatever...
      // Draw the current item text based on the current 
      // Font and the custom brush settings.
      //
      e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
            e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
        // If the ListBox has focus, draw a focus rectangle 
        // around the selected item.
        //
          e.DrawFocusRectangle();
    }
    
    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Sunday, February 13, 2011 5:27 AM
    Moderator