This is a owner draw combobox , that might help you. It contains additional
code to put an image depending on a condition. Ignore this code :)
using System;
using System.Collecti ons;
using System.Windows. Forms;
using System.Data;
using System.Drawing;
using System.Componen tModel;
using System.Drawing. Design;
namespace Framework.Forms
{
/// <summary>
/// Summary description for ImageComboBox.
/// </summary>
[ToolboxBitmap(t ypeof(ComboBox) )]
public class ImageComboBox : ComboBox
{
//Members
private ImageMappingsCo llection imageMappings=n ew
ImageMappingsCo llection();
private ImageList imageList;
//Properties
[DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Content)]
public ImageMappingsCo llection ImageMappings
{
get { return imageMappings; }
set { imageMappings=v alue; }
}
public ImageList ImageList
{
get { return imageList; }
set
{
imageList=value ;
ImageMappings.I mageList=value;
}
}
[Browsable(false )]
public new DrawMode DrawMode
{
get { return base.DrawMode; }
set { base.DrawMode=v alue; }
}
[Browsable(false )]
public new ComboBoxStyle DropDownStyle
{
get { return base.DropDownSt yle; }
set { base.DropDownSt yle=value; }
}
public ImageComboBox()
{
DrawMode=DrawMo de.OwnerDrawFix ed;
DropDownStyle=C omboBoxStyle.Dr opDownList;
DrawItem+=new DrawItemEventHa ndler(ImageComb oBox_DrawItem);
}
private void ImageComboBox_D rawItem(object sender, DrawItemEventAr gs e)
{
e.DrawBackgroun d();
e.DrawFocusRect angle();
Int32 imageIndex=-1;
Boolean isContained;
isContained=fal se;
if ( !(SelectedItem is DataRowView) ) return;
DataRowView row,rowToDraw;
row=(DataRowVie w)SelectedItem;
BindingMemberIn fo bmi=new BindingMemberIn fo(DisplayMembe r);
CurrencyManager cm;
cm=(CurrencyMan ager)BindingCon text[DataSource,bmi. BindingPath];
rowToDraw=(Data RowView)cm.List[e.Index];
Int32 leftIndent=4;
if ( imageList!=null )
{
leftIndent+=ima geList.ImageSiz e.Width;
}
e.Graphics.Draw String(rowToDra w[bmi.BindingFiel d].ToString(),e.F ont,new
SolidBrush(e.Fo reColor),
leftIndent,e.Bo unds.Top+1);
foreach ( ImageMapping mapping in imageMappings )
{
if ( mapping.Conditi on!="" && mapping.Conditi on!=null )
{
DataRow[] matchingRows;
matchingRows=ro wToDraw.DataVie w.Table.Select( mapping.Conditi on);
foreach ( DataRow item in matchingRows )
{
if ( item==rowToDraw .Row )
{
isContained=tru e;
break;
}
}
if ( isContained )
{
imageIndex=mapp ing.ImageIndex;
break;
}
}
}
if ( isContained )
e.Graphics.Draw Image(imageList .Images[imageIndex], new Point(e.Bounds. X,
e.Bounds.Y));
}
}
}
Dan Cimpoiesu
Project Manager
Dion Consulting
"Beeeeeeeeeeeev es" <Be************ **@discussions. microsoft.com> wrote in
message news:63******** *************** ***********@mic rosoft.com...
I have an ownerdrawn combo box which I am drawing with an image and some
text, this is all working beautifully apart from the difference in the Brush
I have to draw the background and the text with if the item is the currently
selected one.
I have selected a blue (SystemColors.H ighlight) background brush and white
(SystemColors.H ighlightText) text brush if the item is the highlighted one,
but when I do that instead of functioning like a normal combobox (when
dropped down) where the items get selected as you mouse over them, as you
mouse over each one, the previously selected (i.e. drawn blue) one *stays
selected* until you scroll.
Obviously I don't want this behaviour. I've checked I haven't got some
multiselect property on or anything. I've written a function which I call to
draw the item unselected which in the OnDrawItem I always call to unselect
the previous item which is a bit of a kludge, but is there a more normal
way? I tried faffing around with the Invalidate method but to no avail.
Oh and another thing is there anyway to get the combo box in its default
mode (i.e. with the text box enabled) to show the picture in the text box,
as it does on drop down list mode (but I want the text box to stay
enabled)... probably not, but just in case! I've got the combo box set to
drop down (the standard, i.e. not 'drop down list' or 'simple')