{"id":1392,"date":"2015-10-09T15:25:41","date_gmt":"2015-10-09T15:25:41","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=1392"},"modified":"2022-02-17T06:24:02","modified_gmt":"2022-02-17T06:24:02","slug":"colouring-combo-box-items","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/combo-box\/colouring-combo-box-items","title":{"rendered":"Colouring Combo Box Items"},"content":{"rendered":"<p>\nSet the ComboBox&nbsp;&#39;Draw Mode&#39; to OwnerDrawFixed and then add this DrawItem event\n<\/p>\n<h4>\nColouring Text<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tprivate: System::Void cmbMyComboBox_DrawItem(System::Object^  sender, System::Windows::Forms::DrawItemEventArgs^  e)\r\n\t{\r\n\t\te-&gt;DrawBackground();\r\n\r\n\t\tif (e-&gt;Index &gt;= 0)\r\n\t\t{\r\n\t\t\tComboBox ^ClickedComboBox = dynamic_cast&lt;ComboBox^&gt;(sender);\r\n\t\t\tString ^text = Convert::ToString(ClickedComboBox-&gt;Items[e->Index]);\r\n\t\t\tSystem::Drawing::Font ^Font1 = ClickedComboBox-&gt;Font;\r\n\r\n\t\t\tSystem::Drawing::Brush ^Brush1;\r\n\t\t\tif (e-&gt;Index == 0)\r\n\t\t\t\tBrush1 = Brushes::Red;\r\n\t\t\telse\r\n\t\t\t\tBrush1 = Brushes::Green;\r\n\r\n\t\t\te-&gt;Graphics-&gt;DrawString(text, Font1, Brush1, Convert::ToSingle(e-&gt;Bounds.X), Convert::ToSingle(e-&gt;Bounds.Y));\r\n\t\t}\r\n\t}\r\n<\/code><\/pre>\n<h4>\nColouring The Background Of Items<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tprivate: System::Void cmbMyComboBox_DrawItem(System::Object^  sender, System::Windows::Forms::DrawItemEventArgs^  e)\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\t\/\/e-&gt;DrawBackground();\r\n\t\t\tif (e.Index == combo.SelectedIndex)\r\n\t\t\t\te-&gt;Graphics-&gt;FillRectangle(gcnew SolidBrush(Color::Gray), e-&gt;Bounds);\r\n\t\t\telse\r\n\t\t\t\te-&gt;Graphics-&gt;FillRectangle(gcnew SolidBrush(Color::White), e-&gt;Bounds);\r\n\t\t}\r\n\t}\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Set the ComboBox&nbsp;&#39;Draw Mode&#39; to OwnerDrawFixed and then add this DrawItem event Colouring Text private: System::Void cmbMyComboBox_DrawItem(System::Object^ sender, System::Windows::Forms::DrawItemEventArgs^ e) { e-&gt;DrawBackground(); if (e-&gt;Index &gt;= 0) { ComboBox ^ClickedComboBox = dynamic_cast&lt;ComboBox^&gt;(sender); String ^text = Convert::ToString(ClickedComboBox-&gt;Items[e->Index]); System::Drawing::Font ^Font1 = ClickedComboBox-&gt;Font; System::Drawing::Brush ^Brush1; if (e-&gt;Index == 0) Brush1 = Brushes::Red; else Brush1 = Brushes::Green; e-&gt;Graphics-&gt;DrawString(text, Font1, Brush1, [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-1392","post","type-post","status-publish","format-standard","hentry","category-combo-box"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=1392"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1392\/revisions"}],"predecessor-version":[{"id":1415,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1392\/revisions\/1415"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=1392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=1392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=1392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}