StrataFrame Forum

ComboBox Item with Graphic

http://forum.strataframe.net/Topic25136.aspx

By Bill Cunnien - 11/6/2009

I have a list of codes in a combobox. Some of the codes are inactive. Can I place a graphic next to a code that is inactive? If so, how do I get there? The combobox is populated by a business object.



Thanks,

Bill
By Greg McGuffey - 11/9/2009

Bill,



I think you have to use owner drawing. I found this at StackOverflow:



http://stackoverflow.com/questions/1286248/drop-down-image-list-in-winforms



Looks like a bit of work, but doable.
By Bill Cunnien - 11/9/2009

That does seem like the direction I need to go. How do I access the BO that I am basing the ComboBox on? I am trying to access the inactive field of the BO during that method--DrawItem.
By Greg McGuffey - 11/9/2009

You'd use the e.Index to access the item in the combo. If I recall (I have to check every time I do this), it is a DataRowView with two columns: name and value. The value is typically setup to be the PK of the item in the BO (its value is determined by the population list settings for the combo). When doing something like this, I'd typically use CopyDataFrom in order to keep data available for this method.



protected override void OnDrawItem(DrawItemEventArgs e)

{

  //-- Get the item, cast to DataRowView.

  var item = this.Items[e.Index] as DataRowView;

  if(item == null) return;



  //-- Get necessary data from BO...assuming used CopyDataFrom

  //   so data is already loaded somewhere.

  int id = (int)item.Item["value"];

  this.MyBO.SeekToPrimaryKey(id);



  //-- Make decisions, draw item....

}

By Bill Cunnien - 11/10/2009

With your suggestion, I was able to manipulate the text's appearance of an individual item. Here is my resulting code:





private void CarrierCBE_DrawItem(object sender, DevExpress.XtraEditors.ListBoxDrawItemEventArgs e)

{

String mCarrierName = (String)e.Item;

if (mCarrierName == null) return;



if (carriersBO1.Seek(String.Format("CarrierName = '{0}'", mCarrierName)))

{

if (carriersBO1.Inactive)

{

e.Appearance.ForeColor = Color.Gray;

e.Appearance.Font = new Font("Tahoma", 8.25F, FontStyle.Italic);

}

else

{

if (carriersBO1.Division == Enumerations.Division.Corporate)

e.Appearance.ForeColor = Color.Blue;

}

}

}





Thanks for your help!!

Bill
By Bill Cunnien - 11/10/2009

(apologies for the lack of code indenting...it was indented when I posted)



Blink
By Bill Cunnien - 11/10/2009

I'll work on introducing a graphic later. I need to bask in the shadow of this mini-victory for a while. w00t
By Greg McGuffey - 11/10/2009

Glad you're getting it figured out. I'll mention that an easier alternative to putting a graphic beside the item is to instead just change the color/font of the text of the item.



As to indenting within code posted here on the forum, the only way I've figured out how to do it is to use the html code for a non-breaking space. However, be warned that if you do a preview, they disappear in the editor window, so copy the text before previewing and paste right after.