Color properties of Controls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All,

While it seems like it should be simple, I am having a problem finding an
answer for this one...

I am building a control where I want one property to be a color. I want the
property to work exactly as any other color property for the standard
controls. That is, I want to be able to put a color name (Red) or HTML/style
string (#FF0000) into the box, and have it persisted that way.

I want the property return type to be Color (rather than, say, string), so
the user can take advantage of using the standard Color pulldowns in the
Properties browser.

Please, show me that it's as simple as it sounds, and I'm just overlooking
something stupid....

Thanks,
PAGates
 
Hi Pagates,

Here's working example that should help you:

using System;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ExampleNamespace.WebControls
{
/// <summary>
///
/// </summary>
[ParseChildren(false)]
[ToolboxData("<{0}:ColorfulLabel runat=server></{0}:Menu>")]
public class ColorfulLabel : WebControl
{

#region Properties

[PersistenceMode(PersistenceMode.Attribute)]
public System.Drawing.Color TextColor
{
get
{
object value = ViewState["TextColor"];
return value == null ? System.Drawing.Color.Empty :
(System.Drawing.Color)value;
}
set
{
ViewState["TextColor"] = value;
}
}

public string Text
{
get
{
string value = (string) ViewState["Text"];
return value == null ? String.Empty : value;
}
set
{
ViewState["Text"] = value;
}
}


#endregion

/// <summary>
///
/// </summary>
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Span;
}
}

/// <summary>
///
/// </summary>
/// <param name="output"></param>
protected override void RenderContents(HtmlTextWriter writer)
{
writer.AddAttribute("color", TextColor.ToString());
writer.RenderBeginTag(HtmlTextWriterTag.Font);
writer.Write(Text);
writer.RenderEndTag();
}
}
}
 
Back
Top