Screen Flickering in AutoSized textbox

148 Views Asked by At

I Have an AutoSized textbox which adjust its size according to the MeasureString of the text inside it by using this code:

private void txt_TextChanged(object sender, EventArgs e)
{
    int xold = txt.Width;
    SizeF MessageSize = txt.CreateGraphics().MeasureString(txt.Text,txt.Font);
    txt.Width = (int)MessageSize.Width + txt.Margin.Left;
    decimal hei = Math.Ceiling((decimal)MessageSize.Height);
    int h = (int)hei;
    txt.Height = h;
    int xnew = txt.Width;
    if(xold > xnew) { txt.Left += xold - xnew; }
    else { txt.Left -= xnew - xold; }
    if (txt.Width == 0) MainPanel.Controls.Remove(txt);
}

it's on a panel that have a background-image so when I start typing it starts flickering a lot. I tried to enable DoubleBufferd from Form_Properties and tried to fix it like:

protected override CreateParams CreateParams
        {
            get
            {
                var parms = base.CreateParams;
                parms.Style &= ~0x02000000;
                return parms;
            }
        }

And it does not seem to help, Any ideas appreciated how to avoid flickering when typing in the textbox. Thanks !

0

There are 0 best solutions below