Click here to Skip to main content
15,867,762 members
Articles / Multimedia / GDI+
Article

A Multi Gradient Button

Rate me:
Please Sign up or sign in to vote.
1.39/5 (11 votes)
15 Dec 20042 min read 71K   1.7K   41   1
Button that allows multiple gradients defined at design time.

Sample Image - gradbutton.jpg

Introduction

First I'd like to thank David Preece for his rainbow button article. Great job, David and it got me thinking! The control has all the features of a regular button and is fully customizable at design time through the properties editor. It also uses a collection of colors (DLL provided) to store the colors.

Background

Why limit the colors to five? So, here is the results of that! A multi colored (minimum of two) gradient button.

How to use the Code

MTMultiGradiantButton can be used in any C# Windows project.

First, add the MTCollections.dll to your project as a resource. Next add MTControls.dll to the toolbox of your Visual Studio or Sharp Develop (the complier I used to create the control). Select the MTMultiGradiantButton control from the file. Then drag and drop the button from the toolbox onto a form. Or you can instantiate the button yourself like this:

C#
private MTControls.MTButtons.MTMultiGradiantButton mTMultiGradiantButton1;
this.mTMultiGradiantButton1= new MTControls.MTButtons.MTMultiGradiantButton();

this.mTMultiGradiantButton1.Colors.Add(System.Drawing.Color.Black);
this.mTMultiGradiantButton1.Colors.Add(System.Drawing.Color.Lime);

or use a different constructor that takes two colors.

C#
private MTControls.MTButtons.MTMultiGradiantButton mTMultiGradiantButton1;
this.mTMultiGradiantButton1 = new MTControls.MTButtons.MTMultiGradiantButton(
System.Drawing.Color.Black, System.Drawing.Color.Lime);

Next, just add the colors you want in two ways:

Using the properties editor, select the Colors property of the button and add your colors through the interface or programmatically add your colors like this:

C#
// this creates a rainbow button with 6 colors
this.mTMultiGradiantButton3 = 
       new MTControls.MTButtons.MTMultiGradiantButton();
this.mTMultiGradiantButton3.Colors.Add(System.Drawing.Color.Red);
this.mTMultiGradiantButton3.Colors.Add(System.Drawing.Color.FromArgb((
      (System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0))));
this.mTMultiGradiantButton3.Colors.Add(System.Drawing.Color.Yellow);
this.mTMultiGradiantButton3.Colors.Add(System.Drawing.Color.Green);
this.mTMultiGradiantButton3.Colors.Add(System.Drawing.Color.Blue);
this.mTMultiGradiantButton3.Colors.Add(System.Drawing.Color.Purple);

Special interest in the code

Activating the ability to use the MTColorCollection in the code was interesting. I needed a dynamic array that would hold color values and be available to set through the property interface at design time. I created the MTColorCollection class that would hold the colors by deriving it from CollectionBase but getting the property interface to use it was as simple as creating a read only property for the MTColorCollection class.

C#
private MTColorCollection m_clrColors = new MTColorCollection();
// This is for the properties interface so you can add colors at design time
// it tells the interface to use the follows MTColorColletion Colors property
// as a template to create the interface for adding colors to the control
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
EditorAttribute("typeof(CollectionEditor)",
           "typeof(System.Drawing.Design.UITypeEditor)")]

public MTColorCollection Colors
{
  get
  {
    return m_clrColors;
  }
}

Finishing up

So go ahead and check out the code. I didn't include project files since I used SharpDevelop and they would be useless to you, Visual Studio .NET users.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionthe source code for MTCollections Pin
zuaretd24-Oct-06 3:37
zuaretd24-Oct-06 3:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.