A blog for developers programming with Autodesk platforms, particularly AutoCAD and Forge. With a special focus on AR/VR and IoT.

March 20, 2008

Implementing your own AutoCAD color combobox control using .NET

A big thanks to Scott McFarlane, from Geotropix, Inc., for sharing the code in this post. Here's an email I received from Scott:

I was reading this blog entry on “Through the Interface” and some folks were asking about how to implement .NET combo box versions of the color and linetype ActiveX controls that are available. I just wanted to share a simple .NET implementation of a color combo box. The color combo is quite easy, really. The linetype one would be more difficult.

Attached is the source code. This is just a generic color combo, that loads up with the 255 ACI colors. It has no dependency on AutoCAD – I was actually using this in an external program. It would be easy, however, to modify this to provide a list item to launch the built-in AutoCAD color dialog if used inside AutoCAD.

Here is the C# code Scott provided (which was in a source file named AcColorComboBox.cs):

using System;

using System.Collections;

using System.Drawing;

using System.Windows.Forms;

publicclass AcColorComboBox : ComboBox

{

publicclass ColorItem

{

privateshort _colorIndex;

private Color _color;

public ColorItem(short colorIndex, Color color)

{

_colorIndex = colorIndex;

_color = color;

}

publicshort ColorIndex

{

get { return _colorIndex; }

}

public Color Color

{

get { return _color; }

}

publicoverridestring ToString()

{

return AcColorComboBox.ColorNameOf(_colorIndex);

}

}

publicclass ColorItemSorter : IComparer

{

publicint Compare(object x, object y)

{

return ((ColorItem)x).ColorIndex - ((ColorItem)y).ColorIndex;

}

}

privateshort _colorIndex;

#region " Windows Form Designer generated code "

public AcColorComboBox()

: base()

{

// This call is required by the Windows Form Designer.

InitializeComponent();

// Add any initialization after the InitializeComponent() call

DrawMode = DrawMode.OwnerDrawFixed;

DropDownStyle = ComboBoxStyle.DropDownList;

}

// Override dispose to clean up the component list.

protectedoverridevoid Dispose(bool disposing)

{

if (disposing)

{

if ((components != null))

{

components.Dispose();

}

}

base.Dispose(disposing);

}

// Required by the Windows Form Designer

private System.ComponentModel.IContainer components;

// NOTE: The following procedure is required by the Windows Form Designer