You could do this with an owner drawn combo box. The LineStyleComboBox class below is one simple example of how to do this. To use this class, add this code to your project, add an instance of the control to your form, add an event handler for the SelectedIndexChanged event, and then set the LineStyle property when the item changes like this:
void OnSelectedIndexChanged(object sender, EventArgs e)
{
// Assuming a WaveformPlot/ScatterPlot plot member and a LineStyleComboBox
// lineStyles member.
plot.LineStyle = lineStyles.CurrentStyle;
}
The attached screenshot demonstrates what this looks like in an application. Hope this helps.
- Elton
using NationalInstruments;
using NationalInstruments.UI;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace NationalInstruments.DeveloperZone.Demos
{
public class LineStyleComboBox : ComboBox
{
// Define default constants for drawing. Customize these if desired.
private const int ItemPadding = 1;
private static readonly Size DefaultPreviewSize = new Size(32, 24);
private static readonly int DefaultTotalHeight = DefaultPreviewSize.Height + (ItemPadding * 2);
// Define default colors and line width. Could also make these properties for
// additional design-time options.
private static readonly Color PreviewBackgroundColor = Color.Black;
private static readonly Color PreviewForegroundColor = Color.LimeGreen;
private const float PreviewLineWidth = 1.0f;
public LineStyleComboBox()
{
// Specify the combo box will be owner drawn so we can draw the line style.
DrawMode = DrawMode.OwnerDrawFixed;
DropDownStyle = ComboBoxStyle.DropDownList;
// Add all of the line styles to the list.
foreach (LineStyle style in EnumObject.GetValues(typeof(LineStyle)))
Items.Add(style);
// Set the selected item to LineStyle.Solid by default.
SelectedItem = LineStyle.Solid;
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
// Make the item height the max of the preview height or the label height.
LineStyle currentItem = GetCurrentItemAtIndex(e.Index);
if (currentItem != null)
{
string label = currentItem.ToString();
int labelHeight = (int)(e.Graphics.MeasureString(label, Font).Height);
e.ItemHeight = Math.Max(DefaultTotalHeight, labelHeight);
}
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
LineStyle currentItem = GetCurrentItemAtIndex(e.Index);
if (currentItem != null)
{
// Draw the background selected if the current item is selected.
if (SelectedItem == currentItem)
e.DrawBackground();
// Calculate the bounds for the line style preview.
Rectangle previewBounds = new Rectangle(
e.Bounds.X + ItemPadding,
e.Bounds.Y + ItemPadding,
DefaultPreviewSize.Width,
e.Bounds.Height - ItemPadding
);
// Fill the background of the line style preview.
using (Brush backgroundBrush = new SolidBrush(PreviewBackgroundColor))
{
e.Graphics.FillRectangle(backgroundBrush, previewBounds);
}
// Draw a preview line for the current line style.
LineStyleDrawArgs styleArgs = new LineStyleDrawArgs(
previewBounds,
PreviewForegroundColor,
PreviewLineWidth
);
using (Pen previewPen = currentItem.CreatePen(this, styleArgs))
{
int yPosition = previewBounds.Y + (previewBounds.Height / 2);
e.Graphics.DrawLine(
previewPen,
new Point(e.Bounds.X, yPosition),
new Point(e.Bounds.X + previewBounds.Width, yPosition)
);
}
// Calculate where to draw the name of the line style.
string label = currentItem.ToString();
SizeF labelSize = e.Graphics.MeasureString(label, e.Font);
float labelX = previewBounds.X + previewBounds.Width + (ItemPadding * 2);
float labelY = e.Bounds.Y + (Math.Abs(e.Bounds.Height - labelSize.Height) / 2);
// Draw the line style label.
using (Brush labelBrush = new SolidBrush(e.ForeColor))
{
e.Graphics.DrawString(label, e.Font, labelBrush, labelX, labelY);
}
}
}
private LineStyle GetCurrentItemAtIndex(int index)
{
LineStyle currentItem = null;
if ((index >= 0) && (index < Items.Count))
currentItem = Items[index] as LineStyle;
return currentItem;
}
// Provide convenience property to get the selected style.
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public LineStyle CurrentStyle
{
get
{
return SelectedItem as LineStyle;
}
}
// Shadow the Items property to hide it from the Windows Forms designer.
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public new ComboBox.ObjectCollection Items
{
get
{
return base.Items;
}
}
}
}