0

enter image description hereI am making a trying to make this combo box: see picture attached. For the LineStyle combo box.

Here's the code I have so far

public partial class frmDlgGraphOptions : Form
      public partial class frmDlgGraphOptions : Form
    {
        public frmDlgGraphOptions()
        {
            InitializeComponent();
            CmbBoxlineStyles.DropDownStyle = ComboBoxStyle.DropDownList;

        }

        public override void OnDrawItem(DrawItemEventArgs e)
        {
             // Get the item.

      var item = this.CmbBoxlineStyles.SelectedIndex.ToString();
      if(item == null)
         return;

      int startX = e.Bounds.X;
      int startY = (e.Bounds.Y + 1);

      int endX = e.Bounds.X + 5;
      int endY = (e.Bounds.Y + 1);


      //Draw the lines 
      Pen pen = new Pen(Color.Blue);

      e.Graphics.DrawLine(pen, new Point(startX, startY), new Point(endX, endY));



        }

    }

I am getting this error: Error 1 'Fdrc.frmDlgGraphOptions.OnDrawItem(System.Windows.Forms.DrawItemEventArgs)': no suitable method found to override

Thank you Sun

LarsTech
  • 80,625
  • 14
  • 153
  • 225
user575219
  • 2,346
  • 15
  • 54
  • 105

3 Answers3

3

The form doesn't have an OnDrawItem event, so there is nothing to override.

Instead, you need to use the DrawItem event of the combobox:

public frmDlgGraphOptions()
{
  InitializeComponent();
  CmbBoxlineStyles.DropDownStyle = ComboBoxStyle.DropDownList;
  CmbBoxlineStyles.DrawMode = DrawMode.OwnerDrawFixed;
  CmbBoxlineStyles.DrawItem += CmbBoxlineStyles_DrawItem;
}

void CmbBoxlineStyles_DrawItem(object sender, DrawItemEventArgs e) {
  // draw
}

Make sure you set the DrawMode property so that the control knows to call your draw method.

If you are trying to make your own ComboBox control that draws those line items, I suspect this might be what you are looking for:

public class MyCombo : ComboBox {

  public MyCombo() {
    this.DropDownStyle = ComboBoxStyle.DropDownList;
    this.DrawMode = DrawMode.OwnerDrawFixed;
  }

  protected override void OnDrawItem(DrawItemEventArgs e) {
    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 (Pen p = new Pen(Color.Blue, (Int32)this.Items[e.Index])) {
        e.Graphics.DrawLine(p, new Point(startX, startY), new Point(endX, endY));
      }
    }
    base.OnDrawItem(e);
  }
}

Then you just add your pen size numbers when using the control:

MyCombo CmbBoxlineStyles = new MyCombo();
CmbBoxlineStyles.Items.Add(1);
CmbBoxlineStyles.Items.Add(2);
CmbBoxlineStyles.Items.Add(3);
CmbBoxlineStyles.Items.Add(4);

Resulting in:

enter image description here

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Thank you. I created a custom combo box. Where should I define the pen types. Is it in the OnDrawitem. I did it like this See Edit. – user575219 May 29 '12 at 21:17
  • @user575219 I rolled your question back to the original question. Please post a new question instead of trying to turn your original question into a conversation. Stack Overflow doesn't work well if the question keeps changing. – LarsTech May 29 '12 at 21:34
  • Hi, I posted another question. Pls take a look at it. Thanks – user575219 May 30 '12 at 06:06
  • @user575219 Because I rolled it back, you *unselect* my post as the answer? Thanks. – LarsTech May 30 '12 at 11:47
0

The Form type doesn't have a OnDrawItem method hence there is nothing to override. In order to override the method you will need to inherit directly from the ComboBox type.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

You are trying to override a method of the Form, but what you need to do is to change the behaviour of the ComboBox control. So either create a descendant ComboBox class and override the method there, or add a event handler to CmbBoxlineStyles.DrawItem event (this can be done using the designer) and implement your code there.

Carsten Schütte
  • 4,408
  • 1
  • 20
  • 24