Simon
Simon

Reputation: 25993

Do Control.SuspendLayout and Control.ResumeLayout keep count?

I can't quite think of how to phrase the question to be precise, but hopefully my meaning will be clear. Do Control.SuspendLayout and Control.ResumeLayout keep count?

To put it another way, If I call SuspendLayout twice, and ResumeLayout once, is layout still suspended?

Upvotes: 5

Views: 1449

Answers (2)

Hans Passant
Hans Passant

Reputation: 942197

There's little reason left to get stuck on a question like this. The source code is available, titled "Reference Source". The best way to get it is with the .NET Mass Downloader. Not every .NET assembly has its source code published, your backup is the venerable Reflector.

Anyhoo, the source code looks roughly like this:

private byte layoutSuspendCount;

public void SuspendLayout() {
  layoutSuspendCount++;
  if (layoutSuspendCount == 1) OnLayoutSuspended();
}

public void ResumeLayout() {
  ResumeLayout(true);
}

public void ResumeLayout(bool performLayout) {
  if (layoutSuspendCount > 0) {
    if (layoutSuspendCount == 1) OnLayoutResuming(performLayout);
    layoutSuspendCount--;
    if (layoutSuspendCount == 0 && performLayout) {
      PerformLayout();
    }
  }
} 

internal void PerformLayout(LayoutEventArgs args) {
  if (layoutSuspendCount > 0) {
    //...
    return;
  }
  //etc...
}

So the answer to your question is: Yes.

Upvotes: 10

Joel Coehoorn
Joel Coehoorn

Reputation: 416121

If I call SuspendLayout twice, and ResumeLayout once, is layout still suspended?

No. Layout is resumed.

Upvotes: -3

Related Questions