ItemColor#region ItemColor publicclass ItemColor { private Color m_ForeColor; private Color m_BackColor; public Color ForeColor { get { return m_ForeColor; } set { m_ForeColor = value; } } #endregion
ItemColorCollection#region ItemColorCollection publicclass ItemColorCollection { private ArrayList m_Items; privateint m_Count; public Color ForeColor; public Color BackColor; publicint Count { set { m_Count = value; } } public ItemColorCollection() { m_Items =new ArrayList(); m_Count =0; } public ItemColor this[int iIndex] { get { /**//*if ( iIndex > m_Count-1 ) { throw new ArgumentOutOfRangeException("Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: iIndex"); }*/ if ( iIndex > m_Items.Count-1 ) { for( int i=m_Items.Count ; i <= iIndex ; ++i ) { m_Items.Add(null); } ItemColor ic =new ItemColor(); m_Items[iIndex] = ic; return ic; } else { if ( m_Items[iIndex] ==null ) { ItemColor ic =new ItemColor(); m_Items[iIndex] = ic; return ic; } else { return (ItemColor)m_Items[iIndex]; } } } } } #endregion
有了这两个辅助类后,我们在写我们的ColorfulListBox,代码如下:
ColorfulListBox#region ColorfulListBox using System; using System.IO; using System.Text; using System.Drawing; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Collections; namespace CustomizeControls { /**////<summary> /// Summary description for ColorfulListBox. ///</summary> [DefaultProperty("Text"), ToolboxData("<{0}:ColorfulListBox runat="server"></{0}:ColorfulListBox>")] publicclass ColorfulListBox : System.Web.UI.WebControls.ListBox { private ItemColorCollection m_ItemsColor; public ColorfulListBox() { m_ItemsColor =new ItemColorCollection(); } public ItemColorCollection ItemsColor { get { m_ItemsColor.Count =this.Items.Count; return m_ItemsColor; } } /**////<summary> /// Render this control to the output parameter specified. ///</summary> ///<param name="output"> The HTML writer to write out to </param> protectedoverridevoid Render(HtmlTextWriter output) { this.RenderBeginTag(output); StringBuilder strbStyle =new StringBuilder(); for( int i=0 ; i <this.Items.Count ; ++i ) { ListItem li =this.Items[i]; output.Write("<option value=""+ li.Value +"""); if ( this.SelectedIndex == i ) { output.Write(" selected="selected""); } /**//*foreach( string strKey in li.Attributes.CssStyle.Keys ) { strbStyle.Append(String.Format("{0}:{1};", strKey, li.Attributes.CssStyle[strKey])); } if ( strbStyle.Length > 0 ) { strbStyle.Insert(0, " style=""); strbStyle.Append("""); output.Write(strbStyle.ToString()); strbStyle.Remove(0, strbStyle.Length); }*/ if ( m_ItemsColor[i] !=null ) { Color cForeColor = m_ItemsColor[i].ForeColor; if ( cForeColor != Color.Empty ) { strbStyle.Append(String.Format("color:{0};", cForeColor.Name)); } Color cBackColor = m_ItemsColor[i].BackColor; if ( cBackColor != Color.Empty ) { strbStyle.Append(String.Format("background-color:{0}", cBackColor.Name)); } if ( strbStyle.Length >0 ) { strbStyle.Insert(0, " style=""); strbStyle.Append("""); output.Write(strbStyle.ToString()); strbStyle.Remove(0, strbStyle.Length); } } output.Write(">"+ li.Text +"</option> "); } this.RenderEndTag(output); } } } #endregion