When exactly all controls on the form are visible?

  • Thread starter Thread starter Julia
  • Start date Start date
J

Julia

Hi,

I need to read some entries from the registry and display them inside a form
The operation can take some times so I would like to be notified when the
form is visible
and only than to run the operation

I dont want to use a thread.


Thanks
 
Julia said:
I need to read some entries from the registry and display them inside a form
The operation can take some times so I would like to be notified when the
form is visible
and only than to run the operation

You could subscribe to the form's Activated (or Paint, alternatively) event
and do something like:

class MyForm : Form
{
bool alreadyActivated = false;

void MainForm_Activated(object sender, System.EventArgs e) {
if (!alreadyActivated) {
RunOperation();
alreadyActivated = true;
}
}

...
}

Since Activated gets raised more than once, you'd have to use the flag as
illustrated.

As far as I know, this is the simplest way of doing what you're after.

Note: if the operation is lengthy (i.e. it would noticeably block GUI
updates), it is good practise to have it run in a background thread.
 
C# Learner said:
You could subscribe to the form's Activated (or Paint, alternatively) event
and do something like:

class MyForm : Form
{
bool alreadyActivated = false;

void MainForm_Activated(object sender, System.EventArgs e) {
if (!alreadyActivated) {
RunOperation();
alreadyActivated = true;
}
}

...
}

One simpler twist on this is to unsubscribe from the Activated event
after the RunOperation() call, rather than keeping an extra flag.
Note: if the operation is lengthy (i.e. it would noticeably block GUI
updates), it is good practise to have it run in a background thread.

Agreed.
 
Back
Top