Colorful ListBox Control(2)

翻译|其它|编辑:郝浩|2005-06-14 12:01:00.000|阅读 1410 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


接上文
有了这两个辅助类后,我们在写我们的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>")]
public class 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>

protected override void 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

编译好我们的ColorfulListBox后,其他项目中引用ColorfulListBox.dll,就可以在工具箱中拖出ColorfulListBox,我们可以这样给一个ListBox设置每个Item的颜色了:

clstb.ItemsColor[3].ForeColor = Color.Red;
clstb.ItemsColor[2].BackColor = Color.Yellow;
clstb.ItemsColor[2].ForeColor = Color.Black;
clstb.ItemsColor[1].ForeColor = Color.Blue;

ColorfulListBox效果示例:
Color:  

That's all.


That's all.
 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP