Ownerdrawn combobox

G

Guest

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')
 
G

Guest

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 said:
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(DesignerSerializationVisibility.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(mapping.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 said:
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')
 
D

Dan Cimpoiesu

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(DesignerSerializationVisibility.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(mapping.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 said:
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')
 
D

Dan Cimpoiesu

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 said:
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 said:
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(DesignerSerializationVisibility.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(mapping.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 said:
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')
 
G

Guest

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 said:
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 said:
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 said:
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(DesignerSerializationVisibility.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(mapping.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


message 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')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top