By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,650 Members | 1,976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,650 IT Pros & Developers. It's quick & easy.

Ownerdrawn combobox

P: n/a
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.Highlight) background brush and white (SystemColors.HighlightText) 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')
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.Collections;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Design;

namespace Framework.Forms
{
/// <summary>
/// Summary description for ImageComboBox.
/// </summary>
[ToolboxBitmap(typeof(ComboBox))]
public class ImageComboBox : ComboBox
{
//Members
private ImageMappingsCollection imageMappings=new
ImageMappingsCollection();
private ImageList imageList;

//Properties
[DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)]
public ImageMappingsCollection ImageMappings
{
get { return imageMappings; }
set { imageMappings=value; }
}

public ImageList ImageList
{
get { return imageList; }
set
{
imageList=value;
ImageMappings.ImageList=value;
}
}

[Browsable(false)]
public new DrawMode DrawMode
{
get { return base.DrawMode; }
set { base.DrawMode=value; }
}

[Browsable(false)]
public new ComboBoxStyle DropDownStyle
{
get { return base.DropDownStyle; }
set { base.DropDownStyle=value; }
}

public ImageComboBox()
{
DrawMode=DrawMode.OwnerDrawFixed;
DropDownStyle=ComboBoxStyle.DropDownList;
DrawItem+=new DrawItemEventHandler(ImageComboBox_DrawItem);
}

private void ImageComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();

Int32 imageIndex=-1;

Boolean isContained;
isContained=false;

if ( !(SelectedItem is DataRowView) ) return;

DataRowView row,rowToDraw;
row=(DataRowView)SelectedItem;

BindingMemberInfo bmi=new BindingMemberInfo(DisplayMember);

CurrencyManager cm;
cm=(CurrencyManager)BindingContext[DataSource,bmi.BindingPath];

rowToDraw=(DataRowView)cm.List[e.Index];

Int32 leftIndent=4;

if ( imageList!=null )
{
leftIndent+=imageList.ImageSize.Width;
}

e.Graphics.DrawString(rowToDraw[bmi.BindingField].ToString(),e.Font,new
SolidBrush(e.ForeColor),
leftIndent,e.Bounds.Top+1);

foreach ( ImageMapping mapping in imageMappings )
{
if ( mapping.Condition!="" && mapping.Condition!=null )
{
DataRow[] matchingRows;
matchingRows=rowToDraw.DataView.Table.Select(mappi ng.Condition);

foreach ( DataRow item in matchingRows )
{
if ( item==rowToDraw.Row )
{
isContained=true;
break;
}
}

if ( isContained )
{
imageIndex=mapping.ImageIndex;
break;
}
}
}
if ( isContained )
e.Graphics.DrawImage(imageList.Images[imageIndex], new Point(e.Bounds.X,
e.Bounds.Y));
}
}
}

Dan Cimpoiesu
Project Manager
Dion Consulting
"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in
message news:63**********************************@microsof t.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.Highlight) background brush and white (SystemColors.HighlightText) 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')
Nov 16 '05 #2

P: n/a
LOOK:
This piece of code
DOESN'T solve my problem
DOESN'T include a definition for whatever the hell an 'ImageMappingsCollection' is
DOESN'T even illustrate your ability as a coder AT ALL, you've probably just copied and pasted it from one of your developers to look cool
DOES waste about 30 seconds of my time, hence pisses me off.
So please if I ask a question, and you CAN'T answer it, as you obviously can't, please sod off and leave me alone - i.e. DON'T make a reply as it makes others think the question has been answered when it hasn't.
"Dan Cimpoiesu" wrote:
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.Collections;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Design;

namespace Framework.Forms
{
/// <summary>
/// Summary description for ImageComboBox.
/// </summary>
[ToolboxBitmap(typeof(ComboBox))]
public class ImageComboBox : ComboBox
{
//Members
private ImageMappingsCollection imageMappings=new
ImageMappingsCollection();
private ImageList imageList;

//Properties
[DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)]
public ImageMappingsCollection ImageMappings
{
get { return imageMappings; }
set { imageMappings=value; }
}

public ImageList ImageList
{
get { return imageList; }
set
{
imageList=value;
ImageMappings.ImageList=value;
}
}

[Browsable(false)]
public new DrawMode DrawMode
{
get { return base.DrawMode; }
set { base.DrawMode=value; }
}

[Browsable(false)]
public new ComboBoxStyle DropDownStyle
{
get { return base.DropDownStyle; }
set { base.DropDownStyle=value; }
}

public ImageComboBox()
{
DrawMode=DrawMode.OwnerDrawFixed;
DropDownStyle=ComboBoxStyle.DropDownList;
DrawItem+=new DrawItemEventHandler(ImageComboBox_DrawItem);
}

private void ImageComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();

Int32 imageIndex=-1;

Boolean isContained;
isContained=false;

if ( !(SelectedItem is DataRowView) ) return;

DataRowView row,rowToDraw;
row=(DataRowView)SelectedItem;

BindingMemberInfo bmi=new BindingMemberInfo(DisplayMember);

CurrencyManager cm;
cm=(CurrencyManager)BindingContext[DataSource,bmi.BindingPath];

rowToDraw=(DataRowView)cm.List[e.Index];

Int32 leftIndent=4;

if ( imageList!=null )
{
leftIndent+=imageList.ImageSize.Width;
}

e.Graphics.DrawString(rowToDraw[bmi.BindingField].ToString(),e.Font,new
SolidBrush(e.ForeColor),
leftIndent,e.Bounds.Top+1);

foreach ( ImageMapping mapping in imageMappings )
{
if ( mapping.Condition!="" && mapping.Condition!=null )
{
DataRow[] matchingRows;
matchingRows=rowToDraw.DataView.Table.Select(mappi ng.Condition);

foreach ( DataRow item in matchingRows )
{
if ( item==rowToDraw.Row )
{
isContained=true;
break;
}
}

if ( isContained )
{
imageIndex=mapping.ImageIndex;
break;
}
}
}
if ( isContained )
e.Graphics.DrawImage(imageList.Images[imageIndex], new Point(e.Bounds.X,
e.Bounds.Y));
}
}
}

Dan Cimpoiesu
Project Manager
Dion Consulting
"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in
message news:63**********************************@microsof t.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.Highlight) background brush and white

(SystemColors.HighlightText) 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')

Nov 16 '05 #3

P: n/a
I just tried to help you. Sorry if I didn't succeed.
I made myself the OwnerDraw ComboBox, and I indeed copy and paste, but not
from my developers code, but from my own code.
I told you to ignore the 'ImageMappingsCollection' that is linked by some
class that I use in my custom framework.
The key is the code contain in the procedure below.
Also you must have the combobox style set to dropdownlist, because setting
to dropdown will not ownerdraw the editbox.

private void ImageComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();

if ( !(SelectedItem is DataRowView) ) return;

DataRowView row,rowToDraw;
row=(DataRowView)SelectedItem;

BindingMemberInfo bmi=new BindingMemberInfo(DisplayMember);

CurrencyManager cm;
cm=(CurrencyManager)BindingContext[DataSource,bmi.BindingPath];

rowToDraw=(DataRowView)cm.List[e.Index];

Int32 leftIndent=4;

e.Graphics.DrawString(rowToDraw[bmi.BindingField].ToString(),e.Font,new
SolidBrush(e.ForeColor),
leftIndent,e.Bounds.Top+1);

e.Graphics.DrawImage(imageList.Images[imageIndex], new Point(e.Bounds.X,
e.Bounds.Y));
}
"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in
message news:BD**********************************@microsof t.com...
LOOK:
This piece of code
DOESN'T solve my problem
DOESN'T include a definition for whatever the hell an 'ImageMappingsCollection' is DOESN'T even illustrate your ability as a coder AT ALL, you've probably just copied and pasted it from one of your developers to look cool DOES waste about 30 seconds of my time, hence pisses me off.
So please if I ask a question, and you CAN'T answer it, as you obviously can't, please sod off and leave me alone - i.e. DON'T make a reply as it
makes others think the question has been answered when it hasn't.

"Dan Cimpoiesu" wrote:
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.Collections;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Design;

namespace Framework.Forms
{
/// <summary>
/// Summary description for ImageComboBox.
/// </summary>
[ToolboxBitmap(typeof(ComboBox))]
public class ImageComboBox : ComboBox
{
//Members
private ImageMappingsCollection imageMappings=new
ImageMappingsCollection();
private ImageList imageList;

//Properties
[DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)] public ImageMappingsCollection ImageMappings
{
get { return imageMappings; }
set { imageMappings=value; }
}

public ImageList ImageList
{
get { return imageList; }
set
{
imageList=value;
ImageMappings.ImageList=value;
}
}

[Browsable(false)]
public new DrawMode DrawMode
{
get { return base.DrawMode; }
set { base.DrawMode=value; }
}

[Browsable(false)]
public new ComboBoxStyle DropDownStyle
{
get { return base.DropDownStyle; }
set { base.DropDownStyle=value; }
}

public ImageComboBox()
{
DrawMode=DrawMode.OwnerDrawFixed;
DropDownStyle=ComboBoxStyle.DropDownList;
DrawItem+=new DrawItemEventHandler(ImageComboBox_DrawItem);
}

private void ImageComboBox_DrawItem(object sender, DrawItemEventArgs e) {
e.DrawBackground();
e.DrawFocusRectangle();

Int32 imageIndex=-1;

Boolean isContained;
isContained=false;

if ( !(SelectedItem is DataRowView) ) return;

DataRowView row,rowToDraw;
row=(DataRowView)SelectedItem;

BindingMemberInfo bmi=new BindingMemberInfo(DisplayMember);

CurrencyManager cm;
cm=(CurrencyManager)BindingContext[DataSource,bmi.BindingPath];

rowToDraw=(DataRowView)cm.List[e.Index];

Int32 leftIndent=4;

if ( imageList!=null )
{
leftIndent+=imageList.ImageSize.Width;
}

e.Graphics.DrawString(rowToDraw[bmi.BindingField].ToString(),e.Font,new SolidBrush(e.ForeColor),
leftIndent,e.Bounds.Top+1);

foreach ( ImageMapping mapping in imageMappings )
{
if ( mapping.Condition!="" && mapping.Condition!=null )
{
DataRow[] matchingRows;
matchingRows=rowToDraw.DataView.Table.Select(mappi ng.Condition);

foreach ( DataRow item in matchingRows )
{
if ( item==rowToDraw.Row )
{
isContained=true;
break;
}
}

if ( isContained )
{
imageIndex=mapping.ImageIndex;
break;
}
}
}
if ( isContained )
e.Graphics.DrawImage(imageList.Images[imageIndex], new Point(e.Bounds.X, e.Bounds.Y));
}
}
}

Dan Cimpoiesu
Project Manager
Dion Consulting
"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in
message news:63**********************************@microsof t.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.Highlight) background brush and
white (SystemColors.HighlightText) 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')

Nov 16 '05 #4

P: n/a
That certainly LOOKS a little more considered...
I'll examine it and see if it works for me.
Sorry if I came across as rude.


"Dan Cimpoiesu" wrote:
I just tried to help you. Sorry if I didn't succeed.
I made myself the OwnerDraw ComboBox, and I indeed copy and paste, but not
from my developers code, but from my own code.
I told you to ignore the 'ImageMappingsCollection' that is linked by some
class that I use in my custom framework.
The key is the code contain in the procedure below.
Also you must have the combobox style set to dropdownlist, because setting
to dropdown will not ownerdraw the editbox.

private void ImageComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();

if ( !(SelectedItem is DataRowView) ) return;

DataRowView row,rowToDraw;
row=(DataRowView)SelectedItem;

BindingMemberInfo bmi=new BindingMemberInfo(DisplayMember);

CurrencyManager cm;
cm=(CurrencyManager)BindingContext[DataSource,bmi.BindingPath];

rowToDraw=(DataRowView)cm.List[e.Index];

Int32 leftIndent=4;

e.Graphics.DrawString(rowToDraw[bmi.BindingField].ToString(),e.Font,new
SolidBrush(e.ForeColor),
leftIndent,e.Bounds.Top+1);

e.Graphics.DrawImage(imageList.Images[imageIndex], new Point(e.Bounds.X,
e.Bounds.Y));
}
"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in
message news:BD**********************************@microsof t.com...
LOOK:
This piece of code
DOESN'T solve my problem
DOESN'T include a definition for whatever the hell an

'ImageMappingsCollection' is
DOESN'T even illustrate your ability as a coder AT ALL, you've probably

just copied and pasted it from one of your developers to look cool
DOES waste about 30 seconds of my time, hence pisses me off.
So please if I ask a question, and you CAN'T answer it, as you obviously

can't, please sod off and leave me alone - i.e. DON'T make a reply as it
makes others think the question has been answered when it hasn't.


"Dan Cimpoiesu" wrote:
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.Collections;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Design;

namespace Framework.Forms
{
/// <summary>
/// Summary description for ImageComboBox.
/// </summary>
[ToolboxBitmap(typeof(ComboBox))]
public class ImageComboBox : ComboBox
{
//Members
private ImageMappingsCollection imageMappings=new
ImageMappingsCollection();
private ImageList imageList;

//Properties
[DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)] public ImageMappingsCollection ImageMappings
{
get { return imageMappings; }
set { imageMappings=value; }
}

public ImageList ImageList
{
get { return imageList; }
set
{
imageList=value;
ImageMappings.ImageList=value;
}
}

[Browsable(false)]
public new DrawMode DrawMode
{
get { return base.DrawMode; }
set { base.DrawMode=value; }
}

[Browsable(false)]
public new ComboBoxStyle DropDownStyle
{
get { return base.DropDownStyle; }
set { base.DropDownStyle=value; }
}

public ImageComboBox()
{
DrawMode=DrawMode.OwnerDrawFixed;
DropDownStyle=ComboBoxStyle.DropDownList;
DrawItem+=new DrawItemEventHandler(ImageComboBox_DrawItem);
}

private void ImageComboBox_DrawItem(object sender, DrawItemEventArgs e) {
e.DrawBackground();
e.DrawFocusRectangle();

Int32 imageIndex=-1;

Boolean isContained;
isContained=false;

if ( !(SelectedItem is DataRowView) ) return;

DataRowView row,rowToDraw;
row=(DataRowView)SelectedItem;

BindingMemberInfo bmi=new BindingMemberInfo(DisplayMember);

CurrencyManager cm;
cm=(CurrencyManager)BindingContext[DataSource,bmi.BindingPath];

rowToDraw=(DataRowView)cm.List[e.Index];

Int32 leftIndent=4;

if ( imageList!=null )
{
leftIndent+=imageList.ImageSize.Width;
}

e.Graphics.DrawString(rowToDraw[bmi.BindingField].ToString(),e.Font,new SolidBrush(e.ForeColor),
leftIndent,e.Bounds.Top+1);

foreach ( ImageMapping mapping in imageMappings )
{
if ( mapping.Condition!="" && mapping.Condition!=null )
{
DataRow[] matchingRows;
matchingRows=rowToDraw.DataView.Table.Select(mappi ng.Condition);

foreach ( DataRow item in matchingRows )
{
if ( item==rowToDraw.Row )
{
isContained=true;
break;
}
}

if ( isContained )
{
imageIndex=mapping.ImageIndex;
break;
}
}
}
if ( isContained )
e.Graphics.DrawImage(imageList.Images[imageIndex], new Point(e.Bounds.X, e.Bounds.Y));
}
}
}

Dan Cimpoiesu
Project Manager
Dion Consulting
"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in
message news:63**********************************@microsof t.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.Highlight) background brush and white (SystemColors.HighlightText) 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')


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.