Asp tutorial. net C # How to pull a color box for custom controls
By inheriting from ComboBox, you can design a color drop-down selection box similar to the C # control attribute bar.
Add component named myColorComboBox. cs
Step 1: inherit from ComboBox and public partial class myColorComboBox: ComboBox
Step 2: Create a drop-down color selection box
Private void InitItems ()
{
This. DrawMode = DrawMode. OwnerDrawFixed; // draw all elements manually.
This. DropDownStyle = ComboBoxStyle. DropDownList; // The drop-down box style cannot be edited.
This. Items. Clear (); // Clear the original Items
Array allColors = Enum. GetValues (typeof (KnownColor); // Obtain the system Color name and save it to the list.
Foreach (KnownColor var in allColors)
{
This. Items. Add (var. ToString (); // load the subitem of the option box
}
This. SelectedIndex = 0;
}
Add InitItems () to the two constructors ()
Step 3: override the OnDrawItem method
Protected override void OnDrawItem (DrawItemEventArgs e)
{
If (e. Index> = 0) // determine whether to re-paint
{
String colorName = this. Items [e. Index]. ToString (); // Obtain the color name.
SolidBrush brush = new SolidBrush (Color. FromName (colorName); // defines the paint brush
Font font = new Font ("", 9); // define the Font
Rectangle rect = e. Bounds;
Rect. Inflate (-2,-2 );
Rectangle rectColor = new Rectangle (rect. Location, new Size (20, rect. Height ));
E. Graphics. FillRectangle (brush, rectColor); // fill color
E. Graphics. DrawRectangle (Pens. Black, rectColor); // draw a border
E. Graphics. DrawString (colorName, font, Brushes. Black, (rect. X + 22), rect. Y); // draw text
}
}
Step 4: Add control attributes
/// <Summary>
/// Selected color name
/// </Summary>
Public string SelectColorName
{
Get {return this. Text ;}
}
/// <Summary>
/// Selected color
/// </Summary>
Public Color SelectColor
{
Get {return Color. FromName (this. Text );}
}
Usage:
Find the custom control myColorComboBox directly from the control bar and drag it to automatically name it myColorCombBox1. You can use myColorCombBox1.SelectColor to obtain the Color and the type is Color. Use myColorCombBox1.SelectColorName to obtain the selected Color name.
The following code is pasted:
// Control name: myColorComboBox
// Author: Liu Dianwu
// Time: 2011-06-01
Using System;
Using System. ComponentModel;
Using System. Collections. Generic;
Using System. Diagnostics;
Using System. Text;
Using System. Windows. Forms;
Using System. Drawing;
Namespace myControl
{
Public partial class myColorComboBox: ComboBox
{
Public myColorComboBox ()
{
InitializeComponent ();
InitItems ();
}
Public myColorComboBox (IContainer container)
{
Container. Add (this );
InitializeComponent ();
InitItems ();
}
Private void InitItems ()
{
This. DrawMode = DrawMode. OwnerDrawFixed; // draw all elements manually.
This. DropDownStyle = ComboBoxStyle. DropDownList; // The drop-down box style cannot be edited.
This. Items. Clear (); // Clear the original Items
Array allColors = Enum. GetValues (typeof (KnownColor); // Obtain the system Color name and save it to the list.
Foreach (KnownColor var in allColors)
{
This. Items. Add (var. ToString (); // load the subitem of the option box
}
This. SelectedIndex = 0;
}
Protected override void OnDrawItem (DrawItemEventArgs e)
{
If (e. Index> = 0) // determine whether to re-paint
{
String colorName = this. Items [e. Index]. ToString (); // Obtain the color name.
SolidBrush brush = new SolidBrush (Color. FromName (colorName); // defines the paint brush
Font font = new Font ("", 9); // define the Font
Rectangle rect = e. Bounds;
Rect. Inflate (-2,-2 );
Rectangle rectColor = new Rectangle (rect. Location, new Size (20, rect. Height ));
E. Graphics. FillRectangle (brush, rectColor); // fill color
E. Graphics. DrawRectangle (Pens. Black, rectColor); // draw a border
E. Graphics. DrawString (colorName, font, Brushes. Black, (rect. X + 22), rect. Y); // draw text
}
}
/// <Summary>
/// Selected color name
/// </Summary>
Public string SelectColorName
{
Get {return this. Text ;}
}
/// <Summary>
/// Selected color
/// </Summary>
Public Color SelectColor
{
Get {return Color. FromName (this. Text );}
}
}
}