我的业务中有许多控件有相同的逻辑,
比如:大部分TextBox和ComboBox都需要根据能否为空设置它的背景色为浅蓝;
还有,如果此控件不能为空,失去焦点时它的背景色也会根据控件内容是否为空改变,不为空则设置默认背景色,为空则显示为黄色背景色提示用户。
还有比较多的其他这方面的逻辑。
我目前的做法是单独写两个基类,一个基类命名为TextBoxEx,它继承自TextBox,然后重写了一些方法和属性等,代码中所有要用到TextBox的地都承自这个基类TextBoxEx,另一个CombBoxEx也按照相似的方法继承重写ComboBox类,所有CombBox都继承这个基类。
但这样的缺点就是大部分代码要写两遍,不但不利于重用,也不利于扩展。
而且如果以后要将这些特性加到其他控件上,又要新建一个基类,将这些代码COPY过去,太不利于扩展。我的目的是:只用在一个地方写一遍这种代码,就能让所有的TextBox和ComboBox继承。但我现在还没找到有什么方法能够同时继承自TextBox和ComboBox两个基类(这样是不可能的吧?)
更没有办法自已新写一个基类同时具有TextBox和ComboBox的特性,因为C#不支持多继承,接口也不是用在这方面吧?
请问我的问题有什么好的解决办法?

解决方案 »

  1.   

    接口可以多继承,但是接口满足不了你的需求。建议,继承比较接近的一个基类,剩余部分只能自己去写了。个人看法,保留;==================================================================
    博客空间:http://blog.csdn.net/lovingkiss
    资源下载:http://download.csdn.net/user/lovingkiss
    Email:loving-kiss@163.com
    本人说明:<我的帖子我做主,结贴率保持100%>
    优惠接单开发,信誉保证,Q64180940(请清楚注明业务还是技术咨询) 
    ==================================================================
      

  2.   

    另外一个办法,制作一个组件,这个组件可以为其它控件提供两个属性为空的背景色,不为空的背景色和一个扩展属性,指定控件是否应用自定义的背景色(类似于tolltip控件提供的扩展属性),.在设计界面时对有相应的控件进行设置,这个方法的好处是无需对基本控件进行扩展,同时可应用于大多数控件.
      

  3.   

    像这种情况应该写个组合控件(TextBox和ComboBox),.net有专门的混合组件的基类,LZ可以继承它,再写上自已的.
      

  4.   

    举个例子
    [ProvideProperty("ActiveEnabled",typeof(Control)]
    public class Activetor:System.Compmentes,System.ComponentModel.IExtenderProvider
    {
    private Color nullColor =Color.Yellow;
    Public Color NullColor()
    {}
    }
      

  5.   

    [ProvideProperty("ActiveEnabled",typeof(Control))]
    public class Activetor:System.ComponentModel.Component,System.ComponentModel.IExtenderProvider
    {
        private Color nullColor =Color.Yellow;
        private Color defaultColor =Color.White;
        private Hashtable Controls;
        private System.ComponentModel.Container components;
        public  Activetor()
        {
            InitializeComponent();
        }
        private void InitializeComponent() 
        {
            this.components = new System.ComponentModel.Container ();
            this.nullColor = Color.Yellow;
            this.defaultColor =Color.White ;
            this.Controls = new Hashtable();
        }    [Browsable(true)]
        public Color NullColor
        {
            get
            {
                return nullColor;
            }
            set 
            {
                nullColor =value;
            }
        }
        [Browsable(true)]
        public Color DefaultColor
        {
            get
            {
                return defaultColor;
            }
            set 
            {
                defaultColor=value;
            }
        }    bool IExtenderProvider.CanExtend(object target) 
        {
            if (target is Control &&!(target is Activetor)) 
            {
                return true;
            }
            return false;
        }
        [DefaultValue(false)]
        public bool GetActiveEnabled(Control control) 
        {
            object v = Controls[control];
            if (v == null) 
            {
                return false;
            }
            return (bool)v;
        }
        public void SetActiveEnabled(Control control, bool value) 
        {
            if (value== false) 
            {
                Controls.Remove(control);
                control.TextChanged  -= new  EventHandler(OnTextChanged);
            }
            else 
            {
                Controls[control] = value;                
                control.TextChanged  += new EventHandler(OnTextChanged);            
            }            
        }
        private void OnTextChanged(object sender, EventArgs e) 
        {
            Control activeControl = (Control)sender;
            if(activeControl.Text =="")
            {
                activeControl.BackColor=nullColor;
            }
            else
            {
                activeControl.BackColor=defaultColor;
            }
        }}
      

  6.   

    赞一下Bote_China(), 稍为修改一下你的code:[ProvideProperty("ActiveEnabled", typeof(Control))]
        public class Activetor : System.ComponentModel.Component, System.ComponentModel.IExtenderProvider
        {
            protected Color nullColor;
            protected Color defaultColor;        protected Dictionary<Control, bool> controls;        protected System.ComponentModel.Container components;        public Activetor()
            {
                InitializeComponent();
            }        protected void InitializeComponent()
            {
                this.components = new Container();
                this.nullColor = Color.LightYellow;
                this.defaultColor = Color.White;
                this.controls = new Dictionary<Control, bool>();
            }        [Browsable(true)]
            public Color NullColor
            {
                get { return nullColor; }
                set { nullColor = value; }
            }        [Browsable(true)]
            public Color DefaultColor
            {
                get { return defaultColor; }
                set { defaultColor = value; }
            }        #region IExtenderProvider Members        bool IExtenderProvider.CanExtend(object extendee)
            {
                if ((extendee is Control) && !(extendee is Activetor))
                    return true;
                return false;
            }        #endregion        [DefaultValue(false)]
            public bool GetActiveEnabled(Control ctrl)
            {
                if (controls.ContainsKey(ctrl))
                    return controls[ctrl];
                else
                    return false;
            }        public void SetActiveEnabled(Control ctrl, bool value)
            {
                if (value)
                {
                    controls[ctrl] = value;
                    ctrl.TextChanged += new EventHandler(OnTextChanged);
                    OnTextChanged(ctrl, new EventArgs());
                }
                else
                {
                    controls.Remove(ctrl);
                    ctrl.TextChanged -= new EventHandler(OnTextChanged);
                }
            }        protected void OnTextChanged(object sender, EventArgs e)
            {
                Control activeCtrl = (Control)sender;
                if (activeCtrl.Text == "")
                {
                    activeCtrl.BackColor = nullColor;
                }
                else
                {
                    activeCtrl.BackColor = defaultColor;
                }
            }
        }