Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

I am a new developer, I try to find information about checking the event on child form while retrieve the data.

Example:

I create the application (MDI form), and I create function to retrieve the data to display on datagridview control of "child form", but while retrieving data has delay a bit.

so I want to check the status of the child form is ready or not, when user click the close button of parent, I want to show a message to warning or error to users such as :"please wait a few while fetching data".

#######

so how do I do? please give me more information

Thank you

My English is not strongest, sorry about it

Godwin11

What I have tried:

If you need picture I will capture later

thank you
Posted
Updated 2-Mar-17 7:02am
v2
Comments
Graeme_Grant 2-Mar-17 7:22am    
There are may ways of doing this. What have you tried so far? Click on Improve question to update with code, error message(s), more information, etc.

You need to expose a property in the child forms. Now the MdiParent can check if it is busy.

The easiest way is to use an interface. This way you can have different types of child forms and the interface makes it easy to interrogate.
C#
interface IFormState
{
    bool IsBusy { get; }
}

Implementing the Interface on the child form(s):
C#
public partial class Form2 : Form, IFormState
{
    public Form2()
    {
        InitializeComponent();
    }

    public bool IsBusy { get; private set; }

    private void butSetBusyState_Click(object sender, EventArgs e)
    {
        IsBusy = true;
    }

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = IsBusy;
    }
}

I have used a button to set the busy state. Also, I've hooked the FormClosing event of the child to also prevent it from closing if busy.

Now for the MDI Parent:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        var form = new Form2();
        form.MdiParent = this;
        form.Show();

    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        foreach (var item in MdiChildren)
        {
            if (((IFormState)item).IsBusy)
            {
                e.Cancel = true;
                break;
            }
        }
    }
}

Like the child forms, the MDI parent hooks the FormClosing event. When fired, will iterate through all the open child forms and cancel the closing if a child form is busy.
 
Share this answer
 
Comments
Member 10368914 24-Jun-17 5:11am    
Sorry for asking late, you can demo project to me?
At most it is an architectural problem to develop some data flow.

You need in the handler of the close button some information about the state of your document. So normally you work with some pointers to get access and than call a state function which gives you the state. In such cases often some "forward declaration" is done to avoid cyclic inclusion of header.

Example
C++
class MyDocument;//forward declaration (without header inclusion)

class MyMainFrame
{
  MyDocument *m_pDocument;//important: only use pointers
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900