void ListControl_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index < 0) { return; } if (this.listbox.Items != null && this.listbox.Items.Count > 0) { if (e.Index != ListBox.NoMatches) { object obj = this.listbox.Items[e.Index]; // handle background if ((e.State & DrawItemState.Selected) != 0) { e.Graphics.FillRectangle(Brushes.LightGray, e.Bounds); ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds); } else { e.DrawBackground(); } int newX = e.Bounds.X; ListControlItem item = obj as ListControlItem; if (item != null) { Image icon = item.RegisteredObject.GetIcon(); using (icon) { if (icon != null) { e.Graphics.DrawImage(icon, e.Bounds.X + 1, e.Bounds.Y + 1, IMAGE_SIZE, IMAGE_SIZE); newX = e.Bounds.Left + IMAGE_SIZE + this.Margin.Right; } } } // check if this is the 'default' value of the list bool isDefault = false; if (this.isDefaultComparer != null) { isDefault = this.isDefaultComparer(obj); } // handle text bool disposeFont = false; string text = obj.ToString(); Font font = e.Font; if (isDefault) { text = text + " [default]"; font = new Font(e.Font, FontStyle.Bold); disposeFont = true; } Rectangle rect = new Rectangle(newX, e.Bounds.Y, e.Bounds.Right - newX, e.Bounds.Height); TextFormatFlags flags = TextFormatFlags.EndEllipsis | TextFormatFlags.NoClipping; TextRenderer.DrawText(e.Graphics, text, font, rect, this.foreColor, flags); if (disposeFont) { font.Dispose(); font = null; } //Utility.WriteLine("drawitem - " + text); } } }
public void AddItem(ListControlItem item) { this.listbox.Items.Add(item); }