سؤال

I have a UserControl with one textbox on it. I wan't to change the control size depending on the font size, so that it doesn't cut off the bottom of the text, but the changes I'm making to the control size are taking no effect at all.

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);
}

When the OnResize event is fired after the property has been set, this.Height remains the original value.

--EDIT--

As it turns out the problem was that I set the Font before the control size, so it get overwritten. But this is happening in the designer of the page where I'm using this control. How can I prevent this from happening in the future?

هل كانت مفيدة؟

المحلول

So in order to fix this overwriting issue i did the following:

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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top