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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器