0

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;
  }
}
whytheq
  • 34,466
  • 65
  • 172
  • 267

2 Answers2

2

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;
  }
}
rene
  • 41,474
  • 78
  • 114
  • 152
1

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.

Community
  • 1
  • 1
Alexandre Borela
  • 1,576
  • 13
  • 21