// PickColor Control 2004 Source Code .... using System; using System.Drawing; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Text; Using system.text.regularExpressions;
namespace WMB.WebControls {[ToolboxBitmap (typeof (DropDownList)), DefaultProperty ( "Text"), ToolboxData ( "<{0}: ColorPick runat = 'server' />")] public class ColorPick: Control, INamingContainer {// WE NEED A TEXTBOX, A SPAN AND A Table of Which We can set or get proties textbox _textbox = new textbox (); label _label = new label (); table maintable = new table ();
[Category ( "Data"), Description ( "Set to true if you want to create the colortables serverside and send them to the browser."), DefaultValue (false)] public bool ShowColors {get {if (ViewState [ "ShowColors" ] == NULL) {Return False; // default value} Else {return (Bool) ViewState ["showcolors"];}} set {if (value) {_Label.style ["Visibility"] = "visible"; Else {_Label.Style ["Visibility"] = "hidden";
Maintable.visible = value; viewstate ["showcolors"] = value;}}
[Category ("Data"), Description ("TextBox text."), DefaultValue ("")] public string text {get {return_textbox.text;} set {color c; if (value.trim ()! = " ") {C = color.fromname (value); if (! C.isknowncolor) {string hexvalue = regex.replace (value," [^ 0-9a-fa-f] "," ")
IF (HexValue.Length == 6) {c = colorTranslator.FromHTML ("#" hexvalue); value = colorTranslator.tohtml (c);} else {c = color.White; value = "#error";}} Else {value = colorTranslator.tohtml (c);}} else {c = color.White;}
IF (c.getBrightness () <0.5) {_textbox.style ["color"] = "white";} else {_textbox.style ["color"] = "black";
_TextBox.text = value; _TextBox.style ["Background-color"] = colorTranslator.tohtml (c);}}
protected override void OnInit (EventArgs e) {// set default visibility of the color tables ShowColors = ShowColors; // the textbox is added here in order to maintain viewstate _TextBox.TextChanged = new EventHandler (TextBox_TextChanged); _TextBox.AutoPostBack = true _TextBox.id = "colorfield"; controls.add (_TextBox); base.onit (e);}
Protected Override Void CreateChildControls () {// First We Edit The Main Span_Label.Style ["Position"] = "Absolute"; _Label.id = "Colorspan";
// We need a table for layout and header MainTable.Attributes.Add ( "style", "color: captiontext; background-color: threedface; border: 2px threedhighlight outset; font-weight: bold; border-collapse: collapse;" );
// this is Our header row tablerow maintr = new tablerow (); maintr.style ["Background-color"] = "ActiveCAption";
// this is the tablecell with the header text TableCell MainTd = new TableCell (); MainTd.Style [ "padding-left"] = "5px"; MainTd.ColumnSpan = 2; MainTd.HorizontalAlign = HorizontalAlign.Left; MainTd.Text = "ColorPick 2004"; maintr.controls.add (maintd);
// this is Tablecell with the closebutton maintd = new Tablecell (); maintd.horizontalalign = horizontalalign.right;
// this is Our closeButton Button CloseButton = New Button (); CloseButton.Text = "X"; CloseButton.attributes.add ("style", "Font-Weight: bold; width: 20px;"); CloseButton.causesvalidation = false; CloseButton.ToolTip = "Close"; CloseButton.Click = new EventHandler (ShowButton_Click); // all add it to our layout table MainTd.Controls.Add (CloseButton); MainTr.Controls.Add (MainTd); MainTable. Controls.add (maintr);
Maintr = new tableerow ();
// lets create u =Table Table ColORTABLE; TABLEROW ColorTr = New TableRow (); Tablecell ColorTd = New TableCell ();
For (int I1 = 0; i1 <= 255; i1 = 51) {maintd = new Tablecell (); colorTable = new table (); for (int I2 = 0; i2 <= 255; i2 = 51) { ColorTr = new tableRow (); for (int i3 = 0; i3 <= 255; i3 = 51) {colorTD = new Tablecell (); button colorbutton = new button (); colorbutton.cssclass = "colorbutton"; colorbutton; color c = Color.FromArgb (i1, i2, i3); ColorButton.Style [ "background-color"] = ColorTranslator.ToHtml (c); ColorButton.ToolTip = ColorButton.Style [ "background-color"]; ColorButton.CausesValidation = false ColorButton.Click = New EventHandler (ColorButton_Click); ColorButton; ColorButton; ColorTr.Controls.Add (ColorTd); } ColORTABLE.CONTROLS.ADD (ColorTr);} maintd.controls.add (colorable); maintr.controls.add (maintd);
IF (i1 == 102 || i1 == 255) {maintable.controls.add (maintr); maintr = new tablerow ();}}
// add the table to _label and _label to control _label.controls.add (maintable); controls.add (_Label);
// let us create and add a button showbutton = new button (); showbutton.id = "showbutton"; showbutton.text = "..."; showbutton.style ["width"] = "20px "; ShowButton.Tooltip =" show colorTables "; showbutton.causesvalidation = false; showbutton.click = new eventhandler (showbutton_click); controls.add (showbutton);}
// Here's Three Functions for the Controls Private Void Showbutton_Click (Object Sender, Eventargs E) {showcolors =! showcolors;
Private Void ColorButton_Click (Object Sender, Eventargs E) {this.text = ((Button) Sender) .Style ["Background-color"]; showcolors = false;}
Private void textbox_textchanged (Object sender, eventargs e) {this.text = _textbox.text;}
// We need LOTS of JavaScript Which We create on prerender protected override void onprender (Eventargs e) {// if we don't have the javascript in in @ yet we'll get it out of cache ... and if it's not there ... we'll create it if (! Page.IsClientScriptBlockRegistered ( "WMBColorPick")) {string ClientScript = (string) HttpContext.Current.Cache.Get ( "WMBColorPick"); if (ClientScript == null) {StringBuilder SB = new stringbuilder (); sb.append ("