Selectively change the color of an Item in a listbox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CSharper

    Selectively change the color of an Item in a listbox

    Is it possible to selectivly change the color of an item in text. I
    saw ForeColor option, but it changes the color of all the items. If it
    is not possible, is there any other control list listbox where we can
    see more than one item and change the color during run time.
    Thanks.
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Selectively change the color of an Item in a listbox

    On Apr 4, 5:47 pm, CSharper <cshar...@gmx.c omwrote:
    Is it possible to selectivly change the color of an item in text. I
    saw ForeColor option, but it changes the color of all the items. If it
    is not possible, is there any other control list listbox where we can
    see more than one item and change the color during run time.
    Thanks.
    IIRC this is not allowed.

    You would have to implement it your self.

    Do a search by owned draw control in opennetcf.org to see an example.
    Even as it's for the CF the same idea applies

    Comment

    • Bart

      #3
      Re: Selectively change the color of an Item in a listbox


      "CSharper" <csharper@gmx.c omschreef in bericht
      news:417466a1-8cde-4306-b048-720e7bf5002a@a2 2g2000hsc.googl egroups.com...
      Is it possible to selectivly change the color of an item in text. I
      saw ForeColor option, but it changes the color of all the items. If it
      is not possible, is there any other control list listbox where we can
      see more than one item and change the color during run time.
      Thanks.


      i think this should do,

      private void PopulateListBox es()
      {

      Updating = true;

      areaListBox.Ite ms.Add(new AreaStyle("Keyw ords", Color.Black,
      Color.White, "Standard") );

      areaListBox.Ite ms.Add(new AreaStyle("Comm ents", Color.Green,
      Color.White, "Standard") );

      areaListBox.Ite ms.Add(new AreaStyle("Numb ers", Color.Navy, Color.Yellow,
      "Bold"));

      areaListBox.Ite ms.Add(new AreaStyle("Stri ngs", Color.Maroon,
      Color.White, "Standard") );

      areaListBox.Ite ms.Add(new AreaStyle("Loca l variables", Color.OrangeRed ,
      Color.White, "Bold"));

      areaListBox.Ite ms.Add(new AreaStyle("Bool ean operators", Color.Black,
      Color.White, "Bold"));

      areaListBox.Ite ms.Add(new AreaStyle("Sign als", Color.CadetBlue ,
      Color.White, "Bold"));

      }



      private void areaListBox_Dra wItem(object sender, DrawItemEventAr gs e)
      {

      ListBox list = (ListBox)sender ;

      AreaStyle style = (AreaStyle)list .Items[e.Index];


      Color foreColor = Color.Empty;

      Color backColor = Color.Empty;

      // Get the Bounding rectangle for a selected item

      Rectangle SelRect = new Rectangle(e.Bou nds.X, e.Bounds.Y,
      e.Bounds.Width - 1, e.Bounds.Height - 1);

      using(Brush backBrush = new SolidBrush(styl e.BackColor))

      {

      // Paint the item background in the wanted color

      e.Graphics.Fill Rectangle(backB rush, SelRect);

      }


      using (Pen p = new Pen(Color.Empty ))

      {

      if(e.State == DrawItemState.S elected)

      {

      // Set the pen color

      p.Color = Color.Black;

      }

      else

      {

      // Set the pen color

      p.Color = list.BackColor;

      }

      // Draw the selection rectangle in either black or the lisbox
      backcolor to hide it

      e.Graphics.Draw Rectangle(p, SelRect);

      }

      e.DrawFocusRect angle();

      using(Brush brush = new SolidBrush(styl e.ForeColor))

      {

      using(Font font = new Font(list.Font,
      GetFonstyleFrom Name(style.Font Style)))

      {

      string text = list.Items[e.Index].ToString();

      e.Graphics.Draw String(text, font, brush, e.Bounds.X, e.Bounds.Y
      + 1);

      }

      }

      }

      private void areaListBox_Mea sureItem(object sender, MeasureItemEven tArgs e)

      {

      // Increase slightly to create some room for the selection rectangle

      e.ItemHeight += 2;

      }

      bart


      Comment

      • Claes Bergefall

        #4
        Re: Selectively change the color of an Item in a listbox

        "CSharper" <csharper@gmx.c omwrote in message
        news:417466a1-8cde-4306-b048-720e7bf5002a@a2 2g2000hsc.googl egroups.com...
        Is it possible to selectivly change the color of an item in text. I
        saw ForeColor option, but it changes the color of all the items. If it
        is not possible, is there any other control list listbox where we can
        see more than one item and change the color during run time.
        Thanks.
        One option is to use a ListView instead. It allows the color to be set
        indvidually for each item.


        /claes


        Comment

        Working...