Asp.net C # custom controls to show the color box

Source: Internet
Author: User

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 );}

        }

    }

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.