MilesDyson
MilesDyson

Reputation: 778

Check if the form is loaded

I have two forms.

. dialogForm has a label in it. When i click a button in MainForm, dialogForm opens. But label in dialogForm is blank. It doesn't have time to load actually. I want to check if the dialogForm fully loaded then proccess can continue in MainForm. For example:

dialogForm tempFrm = new dialogForm();
tempFrm.Show(); // I want to wait till the dialogForm is fully loaded. Then continue to "while" loop.
while(..)
{
...
}

Upvotes: 6

Views: 19499

Answers (6)

Teeracroptus
Teeracroptus

Reputation: 120

You can check the form's Created property.

Gets a value indicating whether the control has been created.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.created?view=windowsdesktop-8.0

Here is how I'm using it to check whether a Form has actually been created before activating it from another thread:

public MyForm()
{

    ...
    
    public void beginInvokeActivate()
    {
        if (!Created) return;
        BeginInvoke(new Action(() =>
        {
            Activate();
        }));
    }
}

Upvotes: 0

Görkem Hacıoğlu
Görkem Hacıoğlu

Reputation: 320

You can use Form.IsActive property.

Or just;

public bool IsFormLoaded;

public MyForm()
    {
        InitializeComponent();
        Load += new System.EventHandler(FormLoaded);
    }
private void FormLoaded(object sender, EventArgs e)
    {
        IsFormLoaded = true;
    }

and check if YourForm.IsFormLoaded, true or false

Upvotes: 0

Viacheslav Smityukh
Viacheslav Smityukh

Reputation: 5833

Your while(...) blocks the UI thread so child form will never got messages and will not be loaded.

To achive you goal you should subscribe to the Load event and continue your code in the handler.

void Click()
{
    var tempFrm = new dialogForm();
    tempFrm.Load += frmLoad;
    tempFrm.Show();
}

void frmLoad(object s, EventArgs ea)
{
   // form loaded continue your code here!
}

Upvotes: 0

Md. Parvez Alam
Md. Parvez Alam

Reputation: 4596

you can try the following

private bool Is_Form_Loaded_Already(string FormName)
            {
                foreach (Form form_loaded in Application.OpenForms)
                {
                    if (form_loaded.Text.IndexOf(FormName) >= 0)
                    {
                        return true;
                    }
                }
                return false;
            }

you can also look in this

Notification when my form is fully loaded in C# (.Net Compact Framework)?

Upvotes: 2

christopher
christopher

Reputation: 27346

Why not create a boolean value, and a method to access it..

private bool Ready = false;

public ConstructorMethod()
{
    // Constructor code etc.
    Ready = true;
}

public bool isReady()
{
    return Ready;
}

Upvotes: 6

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

So you need to consume the forms Shown event:

tempFrm.Shown += (s, e) =>
{
    while(..)
    {
    }
}

But you're going to have another problem. It's going to block the thread. You need to run this while loop on another thread by leveraging a BackgroundWorker or Thread.

Upvotes: 1

Related Questions