• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Questions about some designer code in c# csharp .net

I wanted to create a DataGridView dynamically, and I did so by basically copying most of the designer code.

While doing it I noticed some lines of code that I frankly don't really understand.

Can someone please explain to me what these 3 things do and why? Thanks!


// 1) BeginInit & EndInit
((System.ComponentModel.ISupportInitialize)(this.dgvFile)).BeginInit(); 
((System.ComponentModel.ISupportInitialize)(this.dgvFile)).EndInit();
 
// 2) Resume and Suspend Layout (If 'this' refers to a form,
//    do I need to suspend it if I am not modifying it but
//    only a control within it, so I suspend only that control?)
dgvFile.SuspendLayout(); this.SuspendLayout();
dgvFile.ResumeLayout(false); this.ResumeLayout(false);
 // dgvFile is my DataGridView
 
// 3) this.PerformLayout()?

Open in new window

0
yaronusa
Asked:
yaronusa
  • 3
  • 2
1 Solution
 
Cedric_DCommented:
These blocks used like "cache":

when you issue for ex. several operations, which both cause control's redraw, you will see flicker on the screen.
Include them inside suspendLayout/ResumeLayout  block and it will both remove flicker and increase performance (redraw will be only once at ResumeLayout).

while BeginInit/EndInit used only at initialization of control and its child controls,
BeginLayout/EndLayout can be used through whole control's lifecycle, for example inside event handlers.
0
 
yaronusaAuthor Commented:
You said: "while BeginInit/EndInit used only at initialization of control and its child controls,
BeginLayout/EndLayout can be used through whole control's lifecycle"

Above, are you saying that: although Beg/EndInit is used by the designer at initialization, we can use it later on at runtime?
0
 
yaronusaAuthor Commented:
Ok, thanks for those answers, but I didn't see this part of my questions answered:

"If 'this' refers to a form, do I need to suspend it if I am not modifying it but
 only a control within it, so I suspend only that control?"

In other words, do I need to suspend the container of a control or just the control in order
for the suspension to be effective?
0
 
Cedric_DCommented:
You can suspend any of it, even can suspend no one - this depends on how much you gain with this suspension, for ex.

suspend a form
  -- modify child control 1
  -- modify child control 2
resume form

or

suspend a control
  -- modify some control's property
  -- modify some control's property
resume a control

in my humble opinion, general measure is a count of enclosed operations for necessity of these blocks.
0
 
yaronusaAuthor Commented:
Thanks for your help!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now