14. März 2007

ComboBox Items in verschiedenen Farben

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication7
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private String[] arr;

private Image[] imageArr;
private System.Windows.Forms.ComboBox comboBox1;
public Font myFont;
public Form1()
{
InitializeComponent();
myFont = new System.Drawing.Font("Comic Sans", 11);
arr = new String[4];
arr[0] = "Smiley Red Face";
arr[1] = "Smiley Cry";
arr[2] = "Smiley Big Grin";
arr[3] = "Smiley Suss";
this.comboBox1.DataSource = arr; //set the combo's data source to out array
imageArr = new Image[4];

imageArr[0] = new Bitmap("C:\\smiley_redface.gif");
imageArr[1] = new Bitmap("C:\\smiley_cry.gif");
imageArr[2] = new Bitmap("C:\\smiley_biggrin.gif");
imageArr[3] = new Bitmap("C:\\smiley_suss.gif");
}
//You have your windows forms designer generated code here
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void comboBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
// Let's highlight the currently selected item like any well
// behaved combo box should
e.Graphics.FillRectangle(Brushes.Bisque, e.Bounds);
e.Graphics.DrawString(arr[e.Index], myFont, Brushes.Blue,
new Point(imageArr[e.Index].Width*2,e.Bounds.Y));
e.Graphics.DrawImage(imageArr[e.Index], new Point(e.Bounds.X, e.Bounds.Y));
//is the mouse hovering over a combobox item??
if((e.State & DrawItemState.Focus)==0)
{
//this code keeps the last item drawn from having a Bisque background.
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
e.Graphics.DrawString(arr[e.Index], myFont, Brushes.Blue,
new Point(imageArr[e.Index].Width*2,e.Bounds.Y));
e.Graphics.DrawImage(imageArr[e.Index], new Point(e.Bounds.X, e.Bounds.}
}
}
}

Keine Kommentare: