0

This is an extension of this question C# override OnDrawItem I have made this custom combo box class

class LineStyleComboBox: System.Windows.Forms.ComboBox
{
    Pen SolidPen;
    Pen DashedPen;
    public LineStyleComboBox()
    {
        this.DropDownStyle = ComboBoxStyle.DropDownList;
        this.DrawMode = DrawMode.OwnerDrawFixed;
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        float[] dashValues = { 5, 2, 15, 4 };

         if (e.Index > -1) 
         {

             int startX = e.Bounds.Left + 5;
             int startY = (e.Bounds.Y + e.Bounds.Height / 2);

             int endX = e.Bounds.Right - 5;
             int endY = (e.Bounds.Y + e.Bounds.Height / 2);

             using ( SolidPen = new Pen(Color.Blue, (Int32)this.Items[e.Index]))
             {
                 e.Graphics.DrawLine(SolidPen, new Point(startX, startY), new Point(endX, endY));
             }

             using ( DashedPen = new Pen(Color.Blue, (Int32)this.Items[e.Index]))
             {
                 e.Graphics.DrawLine(DashedPen, new Point(startX, startY), new Point(endX, endY));
                 DashedPen.DashPattern = dashValues;
             }
        }
        base.OnDrawItem(e);
    }
}

In another form where I need to use this LineStyleComboBox, How would I add a collection of pen styles as items. This is so that I can achieve a combo box with line styles as items(solid pen, dashed pen).

private void frmDlgGraphOptions_Load(object sender, EventArgs e)
{
    lineStyleComboBox2.Items.Add(solidpen,dashed pen)
}
Community
  • 1
  • 1
user575219
  • 2,346
  • 15
  • 54
  • 105

1 Answers1

0

You should look at using a WPF control embedded in a WinForms application. You can do about anything you want with a WPF control.

This example is right up your alley. It is a combo-box with custom graphic items.

http://www.switchonthecode.com/tutorials/wpf-tutorial-using-wpf-in-winforms

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69