Colorful ListBox Control

简介:

摘要:
    Asp.net控件中给我们提供了一个ListBox,我们可以通过它在Html文档中产生一个<select></select>Tag,用来选取预先设定的条目(ListItem)。ListBox除了继承来自WebControl之外的成员和属性外,他主要提供了对ListItem集合的包含,由此我们可以获得一个或一组Text、Value和Seleted值。

问题:
    这个系统提供的ListBox控件在操做和功能上能满足我们的需要,因为他本来就是用<select>呈现的,他要实现的东东必然受它限制。但是在外观呈现上,ListBox确实实现的不太令人满意,ListBox给我们提供了ForeColor和BackColor,但是设置这两个属性,整个ListBox的所有选项都变成了同样的ForeColor和BackColor。而ListBox里的ListItem又没有提供ForeColor和BackColor,我曾经试过这样:

None.gif    ListItem.Attributes.CssStyle.Add(“color”, “yellow”);
None.gif    ListItem.Attributes.CssStyle.Add(“background-color”,“blue”);

但是结果没有效果sad_smile.gif

正文:
    所以我们自己实现一个ColorfulListBox,这里有两个方案,我们可以使用前面提到的代码,我们给ListItem添加的CssStyle虽然没有被呈现,但是确实是被加入了的,只是系统带的ListBox在呈现时候把他们忽略了。我们最简单的办法就是继承ListBox,重载Render方法,添加代码:

None.gif     foreachstring strKey  in li.Attributes.CssStyle.Keys )
ExpandedBlockStart.gif    {
InBlock.gif     strbStyle.Append(String.Format("{0}:{1};", strKey,
InBlock.gif      li.Attributes.CssStyle[strKey]));
ExpandedBlockEnd.gif    }
None.gif     if ( strbStyle.Length > 0 )
ExpandedBlockStart.gif    {
InBlock.gif     strbStyle.Insert(0, " style="");
InBlock.gif     strbStyle.Append(""");
InBlock.gif     output.Write(strbStyle.ToString());
InBlock.gif     strbStyle.Remove(0, strbStyle.Length);
ExpandedBlockEnd.gif    }
None.gif

    这样就把ListItem里的CSS手动提取出来了,问题虽然说解决了,可是用起来太麻烦,还要去取一大堆CSS属性,不方便使用,例如我要做第三Item红字的ListBox,我就需要:

None.gif     ColorfulListBox lstb  =  ColorfulListBox();
None.gif    
//  add items
None.gif    
//  dot.gif
None.gif
    lstb.Items[ 2 ].Attributes.CssStyle[”color”]  =  “red”;
None.gif

为了方便使用,我们就再多做一点工作,接下来我们给ListBox添加一个Items颜色列表:

ExpandedBlockStart.gif #region  ItemColor
InBlock.gif 
public   class  ItemColor
ExpandedSubBlockStart.gif 
{
InBlock.gif  
private  Color m_ForeColor;
InBlock.gif  
private  Color m_BackColor; 
InBlock.gif  
public  Color ForeColor
ExpandedSubBlockStart.gif  
{
InBlock.gif   
get
ExpandedSubBlockStart.gif   
{
InBlock.gif    
return  m_ForeColor;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
set
ExpandedSubBlockStart.gif   
{
InBlock.gif    m_ForeColor 
=  value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif
#endregion

ExpandedBlockStart.gif #region  BackColor
InBlock.gif  
public  Color BackColor
ExpandedSubBlockStart.gif  
{
ExpandedSubBlockStart.gif   
get     {     return  m_BackColor;   }
ExpandedSubBlockStart.gif   
set     {    m_BackColor  =  value;   }
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
public  ItemColor()
ExpandedSubBlockStart.gif  
{   SetDefault();  }
InBlock.gif
InBlock.gif  
public   void  SetDefault()
ExpandedSubBlockStart.gif  
{
InBlock.gif   m_ForeColor 
=  Color.Empty;
InBlock.gif   m_BackColor 
=  Color.Empty;
ExpandedSubBlockEnd.gif  }

InBlock.gif }
ExpandedBlockEnd.gif
#endregion

    为了和ListBox的Item集合的访问方法相似,我们在做了一个ItemColorCollection类:

ExpandedBlockStart.gif #region  ItemColorCollection
InBlock.gif 
public   class  ItemColorCollection
ExpandedSubBlockStart.gif 
{
InBlock.gif  
private  ArrayList m_Items;
InBlock.gif  
private   int  m_Count;
InBlock.gif
InBlock.gif  
public  Color ForeColor;
InBlock.gif  
public  Color BackColor;
InBlock.gif
InBlock.gif  
public   int  Count
ExpandedSubBlockStart.gif  
{
InBlock.gif   
set  
ExpandedSubBlockStart.gif   
{
InBlock.gif    m_Count 
=  value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
public  ItemColorCollection()
ExpandedSubBlockStart.gif  
{
InBlock.gif   m_Items 
=   new  ArrayList();
InBlock.gif   m_Count 
=   0 ;
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
public  ItemColor  this [ int  iIndex]
ExpandedSubBlockStart.gif  
{
InBlock.gif   
get
ExpandedSubBlockStart.gif   
{
ExpandedSubBlockStart.gif    
/* if ( iIndex > m_Count-1 )
InBlock.gif    {
InBlock.gif     throw new ArgumentOutOfRangeException("Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: iIndex");
ExpandedSubBlockEnd.gif    }
*/
InBlock.gif    
if  ( iIndex  >  m_Items.Count - 1  )
ExpandedSubBlockStart.gif    
{
InBlock.gif     
for int  i = m_Items.Count ; i  <=  iIndex ;  ++ i )
ExpandedSubBlockStart.gif     
{
InBlock.gif      m_Items.Add(
null );
ExpandedSubBlockEnd.gif     }

InBlock.gif     ItemColor ic 
=   new  ItemColor();
InBlock.gif     m_Items[iIndex] 
=  ic;
InBlock.gif     
return  ic;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gif    
{
InBlock.gif     
if  ( m_Items[iIndex]  ==   null  )
ExpandedSubBlockStart.gif     
{
InBlock.gif      ItemColor ic 
=   new  ItemColor();
InBlock.gif      m_Items[iIndex] 
=  ic;
InBlock.gif      
return  ic;
ExpandedSubBlockEnd.gif     }

InBlock.gif     
else
ExpandedSubBlockStart.gif     
{
InBlock.gif      
return  (ItemColor)m_Items[iIndex];
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif
#endregion

    有了这两个辅助类后,我们在写我们的ColorfulListBox,代码如下:

ExpandedBlockStart.gif #region ColorfulListBox
InBlock.gif using System;
InBlock.gif using System.IO;
InBlock.gif using System.Text;
InBlock.gif using System.Drawing;
InBlock.gif using System.Web.UI;
InBlock.gif using System.Web.UI.WebControls;
InBlock.gif using System.ComponentModel;
InBlock.gif using System.Collections;
InBlock.gif
InBlock.gif namespace CustomizeControls
ExpandedSubBlockStart.gif{
ExpandedSubBlockStart.gif  ///   <summary>
InBlock.gif 
///  Summary description for ColorfulListBox.
ExpandedSubBlockEnd.gif 
///   </summary>
InBlock.gif [DefaultProperty("Text"),
InBlock.gif  ToolboxData("<{0}:ColorfulListBox runat="server"></{0}:ColorfulListBox>")]
InBlock.gif  public  class ColorfulListBox : System.Web.UI.WebControls.ListBox
ExpandedSubBlockStart.gif {
InBlock.gif   private ItemColorCollection m_ItemsColor;
InBlock.gif  
InBlock.gif   public ColorfulListBox()
ExpandedSubBlockStart.gif  {
InBlock.gif   m_ItemsColor =  new ItemColorCollection();
ExpandedSubBlockEnd.gif  }
InBlock.gif
InBlock.gif   public ItemColorCollection ItemsColor
ExpandedSubBlockStart.gif  {
InBlock.gif    get
ExpandedSubBlockStart.gif   {
InBlock.gif    m_ItemsColor.Count =  this.Items.Count;
InBlock.gif     return m_ItemsColor;
ExpandedSubBlockEnd.gif   }
ExpandedSubBlockEnd.gif  }
ExpandedSubBlockStart.gif   ///   <summary>
InBlock.gif  
///  Render this control to the output parameter specified.
InBlock.gif  
///   </summary>
ExpandedSubBlockEnd.gif  
///   <param name="output">  The HTML writer to write out to  </param>
InBlock.gif   protected  override  void Render(HtmlTextWriter output)
ExpandedSubBlockStart.gif  {
InBlock.gif    this.RenderBeginTag(output);
InBlock.gif   StringBuilder strbStyle =  new StringBuilder();
InBlock.gif    forint i=0 ; i <  this.Items.Count ; ++i )
ExpandedSubBlockStart.gif   {
InBlock.gif    ListItem li =  this.Items[i];
InBlock.gif    output.Write("<option value="" + li.Value + """);
InBlock.gif     if (  this.SelectedIndex == i )
ExpandedSubBlockStart.gif    {
InBlock.gif     output.Write(" selected="selected"");
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockStart.gif     /* foreach( string strKey in li.Attributes.CssStyle.Keys )
InBlock.gif    {
InBlock.gif     strbStyle.Append(String.Format("{0}:{1};", strKey,
InBlock.gif      li.Attributes.CssStyle[strKey]));
InBlock.gif    }
InBlock.gif    if ( strbStyle.Length > 0 )
InBlock.gif    {
InBlock.gif     strbStyle.Insert(0, " style="");
InBlock.gif     strbStyle.Append(""");
InBlock.gif     output.Write(strbStyle.ToString());
InBlock.gif     strbStyle.Remove(0, strbStyle.Length);
ExpandedSubBlockEnd.gif    }
*/
InBlock.gif     if ( m_ItemsColor[i] !=  null )
ExpandedSubBlockStart.gif    {
InBlock.gif     Color cForeColor = m_ItemsColor[i].ForeColor;
InBlock.gif      if ( cForeColor != Color.Empty )
ExpandedSubBlockStart.gif     {
InBlock.gif      strbStyle.Append(String.Format("color:{0};", cForeColor.Name));
ExpandedSubBlockEnd.gif     }
InBlock.gif     Color cBackColor = m_ItemsColor[i].BackColor;
InBlock.gif      if ( cBackColor != Color.Empty )
ExpandedSubBlockStart.gif     {
InBlock.gif      strbStyle.Append(String.Format("background-color:{0}", cBackColor.Name));
ExpandedSubBlockEnd.gif     }
InBlock.gif      if ( strbStyle.Length > 0 )
ExpandedSubBlockStart.gif     {
InBlock.gif      strbStyle.Insert(0, " style="");
InBlock.gif      strbStyle.Append(""");
InBlock.gif      output.Write(strbStyle.ToString());
InBlock.gif      strbStyle.Remove(0, strbStyle.Length);
ExpandedSubBlockEnd.gif     }
ExpandedSubBlockEnd.gif    }
InBlock.gif    output.Write(">" + li.Text + "</option> ");
ExpandedSubBlockEnd.gif   }
InBlock.gif    this.RenderEndTag(output);
ExpandedSubBlockEnd.gif  }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif}
ExpandedBlockEnd.gif #endregion

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

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

ColorfulListBox效果示例:

Color: 

 

That's all.


本文转自博客园鸟食轩的博客,原文链接:http://www.cnblogs.com/birdshome/,如需转载请自行联系原博主。

目录
相关文章
|
数据安全/隐私保护
一步一步学Edit Control控件的用法
Edit Control控件最常见的用法,一般有有以下几种: 1、  显示默认的字符串; 2、  接受用户输入的字符串。 3、  作为密码框接受用户输入的字符串。
1064 0
SAP Engagement Center的ShellCarousel控件control
SAP Engagement Center的ShellCarousel控件control
79 0
SAP Engagement Center的ShellCarousel控件control
very important tip - when to add / in binding path Fiori
very important tip - when to add / in binding path Fiori
very important tip - when to add / in binding path Fiori
SAP UI5 Dropdown list binding debugging
Created by Wang, Jerry, last modified on May 20, 2015
100 0
SAP UI5 Dropdown list binding debugging
SAP UI5 Nav container - how the inner control is added
Created by Wang, Jerry, last modified on May 18, 2015
SAP UI5 Nav container - how the inner control is added
Edit Control控件操作问题
1、创建对话框并修改对话框属性 2、创建对话框类 3、为对话框添加控件(Edit Control) 4、添加控件变量(控件属性操作修改控件ID) void Cdemo1Dlg::DoDataExchan...
1011 0