C # override OnDrawItem

enter image description here I am trying to make this combo box: see picture. For the combined LineStyle block.

Here is the code that I still have

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 get this error: Error 1 'Fdrc.frmDlgGraphOptions.OnDrawItem (System.Windows.Forms.DrawItemEventArgs)': no ​​suitable method to override was found

Thanks. Sun

0
source share
3 answers

The Form type does not have an OnDrawItem method, so there is nothing to override. To override a method, you need to inherit directly from the ComboBox type.

0
source

The form does not have an OnDrawItem event, so there is nothing to override.

Instead, you need to use the DrawItem event in the combo box:

 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 can call your draw method.

If you are trying to create your own ComboBox control that draws these positions, 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 simply 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); 

Result:

enter image description here

+3
source

You are trying to override the form method, but you need to change the behavior of the ComboBox control. Therefore, either create a class of ComboBox descendants, either override the method there, or add an event handler to the CmbBoxlineStyles.DrawItem event (this can be done using the constructor) and execute your code there.

0
source

All Articles