【问题标题】:Custom control dynamic size C#自定义控件动态大小 C#
【发布时间】:2014-05-06 05:51:30
【问题描述】:

我有一个用户控件,上面有一个文本框。我不想根据字体大小更改控件大小,这样它就不会切断文本的底部,但是我对控件大小所做的更改根本没有效果。

public override Font Font
{
    get
    {
        return base.Font;
    }
    set
    {
        textBox1.Font = base.Font = value;
        if (Font.Height != this.Height)
            this.Height = Font.Height;
    }
}

protected override void OnResize(EventArgs e)
{
    textBox1.Size = new Size(this.Width - (_textboxMargin * 2 + _borderWidth * 2), this.Height - (_textboxMargin * 2 + _borderWidth * 2));
    textBox1.Location = new Point(_textboxMargin, _textboxMargin);

    base.OnResize(e);
}

在设置属性后触发 OnResize 事件时,this.Height 保持原始值。

--编辑--

事实证明问题是我在控件大小之前设置了字体,所以它被覆盖了。但这发生在我使用此控件的页面的设计器中。我怎样才能防止这种情况在未来发生?

【问题讨论】:

    标签: c# textbox custom-controls font-size


    【解决方案1】:

    所以为了解决这个覆盖问题,我做了以下事情:

    public override Font Font
    {
        get
        {
            return base.Font;
        }
        set
        {
            textBox1.Font = base.Font = value;
            if (Font.Height != this.Height)
            {
                this.Height = Font.Height;
                textBox1.Size = new Size(this.Width - (_textboxMargin * 2 + _borderWidth * 2), this.Height - (_textboxMargin * 2 + _borderWidth * 2));
                textBox1.Location = new Point(_textboxMargin, _textboxMargin);
            }
        }
    }
    
    protected override void OnResize(EventArgs e)
    {
        if (this.Height < Font.Height)
            this.Height = Font.Height;
    
        base.OnResize(e);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多