求范例如何写自定义label,比如新增属性,方法这些!

解决方案 »

  1.   

    具体参考 :http://blog.csdn.net/pbe_sedm/archive/2008/11/22/3349991.aspx
      

  2.   

    我怎么重写TextChanged方法呢,我重写了,竟然直接VSSSTUDIO都关闭了!
    其实我就是想,我传text到Label的TEXT时,我做一下加工,比如给他的TEXT是100,我就加工成100$,怎么写呢
      

  3.   

    参照下面代码,重写TextChanged
    private void label1_TextChanged(object sender, EventArgs e)
    {
        if (!label1.Text.EndsWith("$")) label1.Text = label1.Text + "$";
    }
      

  4.   

    示例:    public partial class MyLabel : Label
        {
            public MyLabel()
            {
                InitializeComponent();
            }        protected override void OnTextChanged(EventArgs e)
            {
                if (!this.Text.EndsWith("$"))
                    this.Text += "$";            base.OnTextChanged(e);
            }
        }
      

  5.   


        public class MyLabel:System.Windows.Forms.Label
        {
            public override string Text
            {
                get
                {
                    return base.Text + "$";
                }
                set
                {
                    base.Text = value;
                }
            }
        }
      

  6.   

    还可以用扩展方法来实现
    因为看到你的需求是从TextBox赋值到label
    所以我就给TextBox扩展了一个方法public static class MyExtensions
        {
            public static string CustomerStyle(this TextBox tb)
            {
                if (string.IsNullOrEmpty(tb.Text))
                    return "";
                if (tb.Text.EndsWith("$"))
                    return tb.Text;            return tb.Text + "$";
            }
        }调用的地方lable.Text=textbox.CustomerStyle();
      

  7.   

    恩。这个MS能行,还有奇怪label的AutoSize我设置为false了为什么我还是不能设置它的大小!求解!
      

  8.   


    这不是设计的时候啊!我在我重写的label里         public MoneyLabel()
            {
             
                this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
                this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.AutoSize = false;
                this.Size = new System.Drawing.Size(120, 20);
                //this.Size = new System.Drawing.Size(120, 20);
            }这个autoSize根本没设成功! 后面的SIZE就也没成功!但是前面两个属性都可以的!
    是原label里那里给autoSize设置了吧,我该在哪里改了?
      

  9.   

    哦。
    控件在设计时会自动生成一些代码(放在:如Form1.Designer.cs 里)
    AutoSize是默认为True的,但是,就算我们new AutoSize,并控制在设计时不要生成AutoSize的代码,刚拉过去时,这个控件显示为“可改变大小(就是控件周围有变改大小的标记)”,但却不起作用,编译一下,控件才真正可以改变大小(且大小为我们设置的初始大小),所以感觉在MS内部代码没处理好。我一般用这个办法处理:    public partial class MyLabel : Label
        {
            public MyLabel()
            {
                InitializeComponent();            this.Size = new Size(200, 50);
            }        bool autoSize = false;
            public new bool AutoSize
            {
                get { return autoSize; }
                set { base.AutoSize=autoSize = value; }
            }        protected override void OnParentChanged(EventArgs e)
            {
                if (this.Parent != null)
                    this.AutoSize = false;            base.OnParentChanged(e);
            }        public new Size Size { get; set; }        protected override void OnTextChanged(EventArgs e)
            {
                if (!this.Text.EndsWith("$"))
                    this.Text += "$";            base.OnTextChanged(e);
            }
        }
      

  10.   

    哦,那个 public new Size Size { get; set; } 不要。