Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ForeColor in Combobox control

ForeColor in Combobox control

Scheduled Pinned Locked Moved C#
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    polishprogrammer
    wrote on last edited by
    #1

    Is there a way to set the ForeColor for only the displayed text in a ComboBox control? In other words, can I set the text of the selected item to, say, Color.Red, while keeping the other items in the list as Color.Black (or any other color I desire)? Thanks.

    M 1 Reply Last reply
    0
    • P polishprogrammer

      Is there a way to set the ForeColor for only the displayed text in a ComboBox control? In other words, can I set the text of the selected item to, say, Color.Red, while keeping the other items in the list as Color.Black (or any other color I desire)? Thanks.

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, Basically you could inherit your own ComboBox from System.Windows.Forms.ComboBox: public class SpecialComboBox : System.Windows.Forms.ComboBox { In the classes constructor you have to change the "DrawMode" property: public SpecialComboBox() { this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; } Then you have to override "OnDrawItem": protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e) { using(System.Drawing.SolidBrush sb = new System.Drawing.SolidBrush(System.Drawing.Color.Red)) { e.Graphics.DrawString(this.GetItemText(this.Items[e.Index]), e.Font, sb, e.Bounds); } base.OnDrawItem (e); } Note: I only tested it with "DropDownStyle" set to "System.Windows.Forms.ComboBoxStyle.DropDown", and I think behaviour changes then. I think this article[^] covers it up better. Hope it helps!

      All the best, Martin

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups