zoukankan      html  css  js  c++  java
  • WinForms中的Label的AutoSize属性

    当大量使用UserControl组合UI时,如果更改了Label的Text属性,Label.AutoSize属性会影响UserControl的OnLoad事件的发生顺序;

    public override string Text
    {
        get
        {
            return base.Text;
        }
        set
        {
            base.Text = value;
        }
    }

    public virtual string Text
    {
        get
        {
            if (!this.CacheTextInternal)
            {
                return this.WindowText;
            }
            if (this.text != null)
            {
                return this.text;
            }
            return "";
        }
        set
        {
            if (value == null)
            {
                value = "";
            }
            if (value == this.Text)
            {
                return;
            }
            if (this.CacheTextInternal)
            {
                this.text = value;
            }
            this.WindowText = value;
            this.OnTextChanged(EventArgs.Empty);
            if (this.IsMnemonicsListenerAxSourced)
            {
                for (Control control = this; control != null; control = control.ParentInternal)
                {
                    Control.ActiveXImpl activeXImpl = (Control.ActiveXImpl)control.Properties.GetObject(Control.PropActiveXImpl);
                    if (activeXImpl != null)
                    {
                        activeXImpl.UpdateAccelTable();
                        return;
                    }
                }
            }
        }
    }

    protected override void OnTextChanged(EventArgs e)
    {
        using (LayoutTransaction.CreateTransactionIf(this.AutoSize, this.ParentInternal, this, PropertyNames.Text))
        {
            this.MeasureTextCache.InvalidateCache();
            base.OnTextChanged(e);
            this.AdjustSize();
            base.Invalidate();
        }
    }

    internal void AdjustSize()
    {
        if (!this.SelfSizing)
        {
            return;
        }
        if (!this.AutoSize && ((this.Anchor & (AnchorStyles.Left | AnchorStyles.Right)) == (AnchorStyles.Left | AnchorStyles.Right) || (this.Anchor & (AnchorStyles.Top | AnchorStyles.Bottom)) == (AnchorStyles.Top | AnchorStyles.Bottom)))
        {
            return;
        }
        int height = this.requestedHeight;
        int width = this.requestedWidth;
        try
        {
            Size size = this.AutoSize ? base.PreferredSize : new Size(width, height);
            base.Size = size;
        }
        finally
        {
            this.requestedHeight = height;
            this.requestedWidth = width;
        }
    }

  • 相关阅读:
    (转)SGI STL中list的sort函数实现
    (转)OpenCv与Qt的结合,几种方法的比较
    (转)typeid详解
    转: C++藏书阁
    (转)Qt多线程编程
    (转)C/C++ 各种计时函数总结
    【转载】深入 Facebook 消息应用服务器
    ubuntu LAMP本地环境配置
    视频: 英语口音纠正课程
    【转载】安装 JDK1.6 / java 1.6 (linux, ubuntu, windows)
  • 原文地址:https://www.cnblogs.com/hubo0831/p/3937867.html
Copyright © 2011-2022 走看看