Reputation: 35557
The following is causing a flicker when the height goes over 810.
How can I prevent this from happening?
private const int EM_GETLINECOUNT = 0xba;
[DllImport("user32",EntryPoint = "SendMessageA",CharSet = CharSet.Ansi,SetLastError = true,ExactSpelling = true)]
private static extern int SendMessage(int hwnd,int wMsg,int wParam,int lParam);
private void rtbScript_TextChanged(object sender,EventArgs e)
{
var numberOfLines = SendMessage(rtbScript.Handle.ToInt32(),EM_GETLINECOUNT,0,0);
this.rtbScript.Height = (rtbScript.Font.Height + 2) * numberOfLines;
if(this.rtbScript.Height>810)
{
this.rtbScript.Height = 810;
}
}
Upvotes: 0
Views: 422
Reputation: 42414
You set the Height
two times, instead of one, causing the Control to repaint itself twice.
To prevent that effect store your calculation of the new height and then assign only once.
private void rtbScript_TextChanged(object sender,EventArgs e)
{
var numberOfLines = SendMessage(rtbScript.Handle.ToInt32(),EM_GETLINECOUNT,0,0);
var newHeight = (rtbScript.Font.Height + 2) * numberOfLines;
if(newHeight>810)
{
this.rtbScript.Height = 810;
}
else
{
this.rtbScript.Height = newHeight;
}
}
Upvotes: 2
Reputation: 1616
Try this: https://stackoverflow.com/a/3718648/5106041 The reason it flickers is because winforms doesn't do double buffering by default, that's one of the reasons WPF was created, not only it fixes these issues (we get some new ones thou) but you'll have a much richer layout system.
Upvotes: 1