[英]Custom control dynamic size C#
我有一個帶有一個文本框的UserControl。 我不會根據字體大小來更改控件的大小,這樣它就不會切斷文本的底部,但是我對控件大小所做的更改完全無效。
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保持原始值。
- 編輯 -
事實證明,問題是我在控件大小之前設置了Font,因此它被覆蓋了。 但這是在我使用此控件的頁面的設計器中發生的。 如何防止這種情況將來發生?
因此,為了解決此覆蓋問題,我做了以下工作:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.