我要写一个web控件库,从ListBox类继承
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
namespace MyControls
{
[DefaultProperty("Text"), 
ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
public class MyControl : System.Web.UI.WebControls.ListBox {
         
         }
}问题是:我需要重写ListBox.Items.Add方法,怎么做?

解决方案 »

  1.   

    public virtual ListItemCollection Items {get;}你首先要重载上述方法.即Items返回的集合类型,然后再根据相应的类型来定义Add方法ListBox.Items.Add 是ListItem下的Add方法,与ListBox类无关
      

  2.   

    感谢stonegoldaustin,但是我还很不明白,是要另建一个新的用户控件,然后继承ListItem类吗?那这两个控件如何统一起来,我需要最后生成一个dll,可以从选项卡中拖到页面上的
      

  3.   

    不好意思,说错一个地方,Add方法是因为ListItemCollection实现了IList接口
      

  4.   

    stonegoldaustin兄,能否麻烦你给个简单例子,对你说的概念我实在不明白,因为C#还没学透,本来就点迷糊,谢谢
      

  5.   

    看来你需要写一个类继承自ListItemCollection,然后重载其中的Add方法,而ListBox的Items必须是从你自定义的ListItemCollection类型
      

  6.   

    是不是自己写一个继承ListItemCollection的类,比如myListItemCollection,然后在myListItemCollection类中重载Add方法,然后在myListBox类中,重载ListBox.Items为myListItemCollection,那么myListBox.items就具有了新的Add方法?
      

  7.   

    如果我上面说的正确,那么如何重载ListBox.Items为myListItemCollection呢