在以下的代码中,我是要实现一个带下边框的编辑控件,并且在最右边带有一个按钮,其它的就不说了,但父类中的几个属性我想重写,如Text属性,我按代码中的样式进行了重写,编译通过,但在使用这个控件时,在属性窗口中却没显示出Text属性,我把属性名改为Text1,然后去掉override关键字,重新编译后,在属性窗口就可以看到这个Text1属性。但我就想重写Text属性,各位看看我该怎么做呢?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;namespace CS1
{
    public partial class UserControl1 : UserControl
    {        protected String txttitle;
        protected bool hasbtn=true;        public UserControl1()
        {
            InitializeComponent();
           // textBox1.BackColor = this.Parent.BackColor;
        }        private void UserControl1_Paint(object sender, PaintEventArgs e)
        {
            Graphics dc = e.Graphics;
            Pen p = new Pen ( Color.Blue );            if (hasbtn)
            {
                dc.DrawLine(p, TextTitle.Width + 1, 16, TextTitle.Width + textBox1.Width, 16);
            }
            else
            {
                dc.DrawLine(p, TextTitle.Width + 1, 16, this.Width, 16);
            }
             }        private void UserControl1_Resize(object sender, EventArgs e)
        {            this.Height = 18;            //设置布局
            Graphics dc = TextTitle.CreateGraphics();
            dc.PageUnit = GraphicsUnit.Pixel;
            SizeF s = dc.MeasureString(TextTitle.Text, TextTitle.Font);            Size textsize = new Size((int)s.Width, (int)s.Height);
            TextTitle.Location = new Point(0, 0);
            TextTitle.Size = new Size(textsize.Width, 14);            if (hasbtn)
            {
                textBox1.Location = new Point(textsize.Width + 1, 0);
                textBox1.Size = new Size(this.Width - btnSelect.Width - TextTitle.Width, 14);                btnSelect.Location = new Point(this.Width - btnSelect.Width, 0);
            }
            else
            {
                textBox1.Location = new Point(textsize.Width + 1, 0);
                textBox1.Size = new Size(this.Width - textsize.Width, 14);
            }           
        }        private void UserControl1_Layout(object sender, LayoutEventArgs e)
        {            this.Height = 18;            //设置布局
            Graphics dc = TextTitle.CreateGraphics() ;
            dc.PageUnit = GraphicsUnit.Pixel;
            SizeF s =dc.MeasureString(TextTitle.Text, TextTitle.Font);            Size textsize = new Size((int)s.Width , (int)s.Height );
            TextTitle.Location = new Point(0, 0);
            TextTitle.Size=new Size(  textsize.Width , 14 );            if (hasbtn)
            {
                textBox1.Location = new Point(textsize.Width + 1, 0 );
                textBox1.Size = new Size( this.Width- btnSelect.Width - TextTitle.Width, 14);                btnSelect.Location = new Point(this.Width - btnSelect.Width, 0);
            }
            else
            {
                textBox1.Location = new Point(textsize.Width + 1, 0);
                textBox1.Size = new Size(this.Width - textsize.Width, 14);
            }        }
        public  Color BackColor1
        {
            get
            {
                return this.BackColor;
            }
            set
            {
                this.BackColor = value;
                textBox1.BackColor = value;
            }
        }        [Description("设置控件标题"), Category("外观")]
        public String TxtTitle
        {
            get
            {
                return TextTitle.Text;
            }
            set
            {
                TextTitle.Text = value;
            }
        }        [DefaultValue("true"),Description("设置是否有选择在右边"), Category("外观")]
        public bool HasSelectButton
        {
            get
            {
                return hasbtn;
            }
            set
            {
                btnSelect.Visible = value;
          
                hasbtn = value;
            }
        }
        [DefaultValue("Text1"),Description("设置编辑器文本"),Category("数据")]
        public  override String Text
        {
            get
            {
                return textBox1.Text;
            }
            set
            {
                textBox1.Text = value;
            }
        }
    }
}

解决方案 »

  1.   

    have a try![DefaultValue("Text"),Description("设置编辑器文本"),Category("数据")]
    public new String Text
    {
        get
        {
             return textBox1.Text;
         }
         set
         {
              textBox1.Text = value;
          }
    }
      

  2.   

    [Browsable(true)]
           [DefaultValue("Text"),Description("设置编辑器文本"),Category("数据")]
            public  override string Text
            {
                get
                {
                    return textBox1.Text;
                }
                set
                {
                    textBox1.Text = value;
                }
            }
    就OK了,Browsable(true)很管用,只是弄不明白为什么一定要加这个。
    另外如果加上[BindableAttribute(true)]属性,则可以执行数据绑定.微软的技术弄死人啊