This is a different approach to what I was doing earlier.
In my combo owner drawn combo box draw 3 lines(solid,dash,dashdot) to be drawn with the color selected from an earlier colpr picker drop down
this.DrawMode = DrawMode.OwnerDrawVariable;
this.DropDownStyle = ComboBoxStyle.DropDownList;
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
int startX = e.Bounds.Left + 5;
int startY = (e.Bounds.Y);
Point p1=new Point(startX,startY);
int endX = e.Bounds.Right - 5;
int endY = (e.Bounds.Y);
ComboBoxItem item = (ComboBoxItem)this.Items[e.Index];
Point p2=new Point(endX,endY);
base.OnDrawItem(e);
Pen SolidmyPen = new Pen(item.foreColor, 1);
Pen DashedPen = new Pen(item.foreColor, 1);
DashedPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
Pen DashDot = new Pen(item.foreColor, 1);
DashDot.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
// Pen DashedPen = new Pen(item.foreColor, (Int32)this.Items[e.Index]);
Bitmap myBitmap = new Bitmap(item.Picture);
Graphics graphicsObj;
graphicsObj = Graphics.FromImage(myBitmap);
switch (e.Index)
{
case 0:
graphicsObj.DrawLine(SolidmyPen, p1, p2);
break;
case 1:
graphicsObj.DrawLine(DashedPen, p1, p2);
break;
case 2:
graphicsObj.DrawLine(DashDot, p1, p2);
break;
}
Here’s what I’m trying to do. Draw 3lines(solid,dash,dashdot) in the combo box.
I don’t see any lines in the combo box except some blue which is the selected color
Thank u
Try this.
I started a new winforms application. Created a class based off of ComboBox added your code and modified it a bit. I think you major problem was in your bitmap part. You create a new bitmap, then draw on that, but you never use the bitmap you created. If you wish to keep the code you created you would have to add to the end of the method item.Picture=myBitmap. But I think that would call the ondrawitem again and you would be in an infinite loop. Instead of creating a graphics object based off the item.Picture, just use the graphics object created for you in the DrawItemEventArgs.
here is what I did, I think I cleaned it up a bit. And you may already know but you should always wrap pens, brushes and graphics in using {….} like I demonstrated below.