Combo box items ( text with images)

S

sajin

Hi All,

I am new to windows forms C# 2005, How can i have a combo box with
image and text

meaning

* 1Star
** 2Start
*** 3Star

i have experimented something. but it shows only images.

Any help will be appreciated

Regards
-Sajin
 
M

Morten Wennevik [C# MVP]

Hi Sajin,

You need to specify DrawMode. OwnerDrawFixed is usually good enough, but use OwnerDrawVariable if the height of the items can vary.

Set DropDownStyle to DropDownList to be able to draw the selected item in the EditBox

Then subscribe to the ComboBox' DrawItem event. It will be called for each item in the list. Use the DrawItemEventArgs to find out what kind of item you are currently drawing and use DrawItemEventArgs.Bounds to position your drawing. You can then draw image and text any way you like. DrawItemEventArgs.State has information of wether the current item is highligted and so on.

If you need more specific information, please ask.
 
S

sajin

Hi Morten,

I did as u said, but still i can't show image with text :-(

I added a imageList and added the images in that and assiciated with
combobox , where am i going wrong
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string[] items;

items = new string[ComboBoxImageList.Images.Count];

for (int i = 0; i < ComboBoxImageList.Images.Count; i++)
{
this.comboBox1.Items.Add(ComboBoxImageList.Images);

}


this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
this.comboBox1.ItemHeight =
this.ComboBoxImageList.ImageSize.Height;
this.comboBox1.Width =
this.ComboBoxImageList.ImageSize.Width + 80;
this.comboBox1.MaxDropDownItems =
this.ComboBoxImageList.Images.Count;
}

private void comboBox1_DrawItem(object sender,
DrawItemEventArgs e)
{
if (e.Index != -1)
{

e.Graphics.DrawImage(this.ComboBoxImageList.Images[e.Index],
e.Bounds.Left, e.Bounds.Top);
}
}
}
}



Regards
-Sajin


Hi Sajin,

You need to specify DrawMode. OwnerDrawFixed is usually good enough, but use OwnerDrawVariable if the height of the items can vary.

Set DropDownStyle to DropDownList to be able to draw the selected item in the EditBox

Then subscribe to the ComboBox' DrawItem event. It will be called for each item in the list. Use the DrawItemEventArgs to find out what kind of item you are currently drawing and use DrawItemEventArgs.Bounds to position your drawing. You can then draw image and text any way you like. DrawItemEventArgs.State has information of wether the current item is highligted and so on.

If you need more specific information, please ask.





I am new to windows forms C# 2005, How can i have a combo box with
image and text

* 1Star
** 2Start
*** 3Star
i have experimented something. but it shows only images.
Any help will be appreciated
Regards
-Sajin

--
Happy coding!
Morten Wennevik [C# MVP]- Hide quoted text -

- Show quoted text -
 
M

Moty Michaely

Hi Morten,

I did as u said, but still i can't show image with text :-(

I added a imageList and added the images in that and assiciated with
combobox , where am i going wrong
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string[] items;

items = new string[ComboBoxImageList.Images.Count];

for (int i = 0; i < ComboBoxImageList.Images.Count; i++)
{
this.comboBox1.Items.Add(ComboBoxImageList.Images);

}

this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
this.comboBox1.ItemHeight =
this.ComboBoxImageList.ImageSize.Height;
this.comboBox1.Width =
this.ComboBoxImageList.ImageSize.Width + 80;
this.comboBox1.MaxDropDownItems =
this.ComboBoxImageList.Images.Count;
}

private void comboBox1_DrawItem(object sender,
DrawItemEventArgs e)
{
if (e.Index != -1)
{

e.Graphics.DrawImage(this.ComboBoxImageList.Images[e.Index],
e.Bounds.Left, e.Bounds.Top);
}
}
}

}

Regards
-Sajin

Hi Sajin,
You need to specify DrawMode. OwnerDrawFixed is usually good enough, but use OwnerDrawVariable if the height of the items can vary.
Set DropDownStyle to DropDownList to be able to draw the selected item in the EditBox
Then subscribe to the ComboBox' DrawItem event. It will be called for each item in the list. Use the DrawItemEventArgs to find out what kind of item you are currently drawing and use DrawItemEventArgs.Bounds to position your drawing. You can then draw image and text any way you like. DrawItemEventArgs.State has information of wether the current item is highligted and so on.
If you need more specific information, please ask.
- Show quoted text -


Sajin

I can't see where the text of an item is added? You are adding an
Image item to the combo box items list.
I think that Morten suggested to draw both the image and the text
(using DrawText()).

Therfore, the item added to the list should contain information about
both the image and the text to be displayed. Maybe you can use a
wrapper class to hold the item information.

Feel free to ask any question regarding the custom drawing issue.

Moty
 
S

sajin

Thanks a ton to Morten and Moty....
It works fine. :))

Regards
-Sajin





Hi Morten,
I did as u said, but still i can't show image with text :-(
I added a imageList and added the images in that and assiciated with
combobox , where am i going wrong
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] items;
items = new string[ComboBoxImageList.Images.Count];
for (int i = 0; i < ComboBoxImageList.Images.Count; i++)
{
this.comboBox1.Items.Add(ComboBoxImageList.Images);

this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
this.comboBox1.ItemHeight =
this.ComboBoxImageList.ImageSize.Height;
this.comboBox1.Width =
this.ComboBoxImageList.ImageSize.Width + 80;
this.comboBox1.MaxDropDownItems =
this.ComboBoxImageList.Images.Count;
}

private void comboBox1_DrawItem(object sender,
DrawItemEventArgs e)
{
if (e.Index != -1)
{
e.Graphics.DrawImage(this.ComboBoxImageList.Images[e.Index],
e.Bounds.Left, e.Bounds.Top);
}
}
}
Regards
-Sajin
Hi Sajin,
You need to specify DrawMode. OwnerDrawFixed is usually good enough, but use OwnerDrawVariable if the height of the items can vary.
Set DropDownStyle to DropDownList to be able to draw the selected item in the EditBox
Then subscribe to the ComboBox' DrawItem event. It will be called for each item in the list. Use the DrawItemEventArgs to find out what kind of item you are currently drawing and use DrawItemEventArgs.Bounds to position your drawing. You can then draw image and text any way you like. DrawItemEventArgs.State has information of wether the current item is highligted and so on.
If you need more specific information, please ask.
Hi All,
I am new to windows forms C# 2005, How can i have a combo box with
image and text
meaning
* 1Star
** 2Start
*** 3Star
i have experimented something. but it shows only images.
Any help will be appreciated
Regards
-Sajin

Sajin

I can't see where the text of an item is added? You are adding an
Image item to the combo box items list.
I think that Morten suggested to draw both the image and the text
(using DrawText()).

Therfore, the item added to the list should contain information about
both the image and the text to be displayed. Maybe you can use a
wrapper class to hold the item information.

Feel free to ask any question regarding the custom drawing issue.

Moty- Hide quoted text -

- Show quoted text -
 
Joined
Feb 23, 2011
Messages
3
Reaction score
0
Hi,

can you please tell me , how you get both text and image.

Thanks a ton to Morten and Moty....
It works fine. :))

Regards
-Sajin





On May 21, 1:05 pm, Moty Michaely <[email protected]> wrote:
> On May 21, 10:27 am, sajin <[email protected]> wrote:
>
>
>
>
>
> > Hi Morten,
>
> > I did as u said, but still i can't show image with text :-(
>
> > I added a imageList and added the images in that and assiciated with
> > combobox , where am i going wrong
> > using System;
> > using System.Collections.Generic;
> > using System.ComponentModel;
> > using System.Data;
> > using System.Drawing;
> > using System.Text;
> > using System.Windows.Forms;
>
> > namespace WindowsApplication1
> > {
> > public partial class Form1 : Form
> > {
> > public Form1()
> > {
> > InitializeComponent();
> > }
>
> > private void Form1_Load(object sender, EventArgs e)
> > {
> > string[] items;
>
> > items = new string[ComboBoxImageList.Images.Count];
>
> > for (int i = 0; i < ComboBoxImageList.Images.Count; i++)
> > {
> > this.comboBox1.Items.Add(ComboBoxImageList.Images);
>
> > }
>
> > this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
> > this.comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
> > this.comboBox1.ItemHeight =
> > this.ComboBoxImageList.ImageSize.Height;
> > this.comboBox1.Width =
> > this.ComboBoxImageList.ImageSize.Width + 80;
> > this.comboBox1.MaxDropDownItems =
> > this.ComboBoxImageList.Images.Count;
> > }
>
> > private void comboBox1_DrawItem(object sender,
> > DrawItemEventArgs e)
> > {
> > if (e.Index != -1)
> > {
>
> > e.Graphics.DrawImage(this.ComboBoxImageList.Images[e.Index],
> > e.Bounds.Left, e.Bounds.Top);
> > }
> > }
> > }
>
> > }
>
> > Regards
> > -Sajin
>
> > On May 18, 9:45 pm, "Morten Wennevik [C# MVP]"
>
> > <[email protected]> wrote:
> > > Hi Sajin,
>
> > > You need to specify DrawMode. OwnerDrawFixed is usually good enough, but use OwnerDrawVariable if the height of the items can vary.
>
> > > Set DropDownStyle to DropDownList to be able to draw the selected item in the EditBox
>
> > > Then subscribe to the ComboBox' DrawItem event. It will be called for each item in the list. Use the DrawItemEventArgs to find out what kind of item you are currently drawing and use DrawItemEventArgs.Bounds to position your drawing. You can then draw image and text any way you like. DrawItemEventArgs.State has information of wether the current item is highligted and so on.
>
> > > If you need more specific information, please ask.
>
> > > On Fri, 18 May 2007 09:12:50 +0200, sajin <[email protected]> wrote:
> > > > Hi All,

>
> > > > I am new to windows forms C# 2005, How can i have a combo box with
> > > > image and text

>
> > > > meaning
>
> > > > * 1Star
> > > > ** 2Start
> > > > *** 3Star

>
> > > > i have experimented something. but it shows only images.
>
> > > > Any help will be appreciated
>
> > > > Regards
> > > > -Sajin

>
> > > --
> > > Happy coding!
> > > Morten Wennevik [C# MVP]- Hide quoted text -

>
> > > - Show quoted text -
>
> Sajin
>
> I can't see where the text of an item is added? You are adding an
> Image item to the combo box items list.
> I think that Morten suggested to draw both the image and the text
> (using DrawText()).
>
> Therfore, the item added to the list should contain information about
> both the image and the text to be displayed. Maybe you can use a
> wrapper class to hold the item information.
>
> Feel free to ask any question regarding the custom drawing issue.
>
> Moty- Hide quoted text -
>
> - Show quoted text -
 
Joined
Feb 23, 2011
Messages
3
Reaction score
0
when i click image event will be fired and displat some data(but not when i click on text)
how it will possible.........
when i click on image combobox events was fired...........
 
Last edited:

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