views:

299

answers:

2

I'm not sure if a component exists for this or if I'll have to look at how to create one, but here goes...

I'm trying to find a dropdown-style list for images I can use in a program of mine. It's fairly simple, just needs to display a few images in a grid with tooltips for each one, and I need to be able to get whichever one was last picked. For example, http://img.photobucket.com/albums/v714/darkflight_devil/ChartType.png without the tab bar in it. Unfortunately my monetary budget is zero which means I can't purchase any controls- are there any free ones like this, or will I need to work at making my own?

If the answer is the latter, are there any useful links you might be able to give me so I could start to work on a control for this?

+1  A: 

There are lots implementations of custom image drop-down lists out there.

You should take a look at the DrawMode property of the ComboBox.

Here's a simple example that implements the use of an ImageList, by inheriting from ComboBox, setting the DrawMode to OwnerDrawFixed and drawing its elements in OnDrawItem:

ComboBox With Images

I'm not sure if this approach enables you to implement the grid-style image list that you mention, but take a look at the example - I'm sure it fits your needs just fine.

Bernhof
+1  A: 

You can inherit your class from System.Windows.Forms.ComboBox and override the protected method OnDrawItem(DrawItemEventArgs e)

Example code:

public class ImageComboBox : ComboBox
{
   protected override void OnDrawItem(DrawItemEventArgs e)
   {
      // Get the item.
      var item = this.Items[e.Index] as string;
      if(item == null)
         return;

      // Get the coordinates to where to draw the image.
      int imageX = e.Bounds.X + 5;
      int imageY = (e.Bounds.Height - image.Height) / 2;

      // Draw image
      e.Graphics.DrawImage(image, new Point(imageX, imageY));

      // Draw text
      e.Graphics.DrawString(item, this.Font, new SolidBrush(Color.Black),
            new PointF(textX, textY);
   }
}

The code above is only a quick and dirty example, and should not be used as it is (should not for example create a new SolidBrush each time the item is drawn), but I hope it will give you an idea of how to do it.

Patrik