简体   繁体   中英

C# - AutoCompleteTextBox open in my Application.OpenForms

Basically what I am trying to do is to check if there is other forms open when closing the main form of my application.

I thought I made it with this code :

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
        if (Application.OpenForms.Count > 1)
        {
            if (!YesNoBox.Show("Title", "Message", this))
            {
                e.Cancel = true;
            }
        }
}

The YesNoBox component is just a classic Yes/No form question and the .Show() returns the answer.

It worked quite good until I added some AutoCompleteTextBox in my other forms..

When I'm using an AutoCompleteTextBox , I fill it with my data when loading the form.

If I click in it, it'll open the list, just like I want, but if I'm closing the form where the AutoCompleteTextBox is, it's still appears in my Application.OpenForms.Count , here's an example.

Of course, if I don't interfere with any AutoCompleteTextBox , the count will be right.

In this case, I only have my MainForm which is open but my Application.OpenForms.Count is 3 so it makes the popup show when closing the MainForm.

I looked at the different members I can find but none of them seems helpful. I don't know why the AutoCompleteTextBox are not closing with their parent.

So, I'm looking for a way to ignore those AutoCompleteTextBox in my test, or correctly close them, or anything else if you have a better way yo do it!

Hope you can help me guys!

Thanks.

EDIT: AutoCompleteTextBox is actually a custom user control that my firm used; you can find the code here on CodeProject. .

Use this code to get count:

int count = 0;
for (int i = 0; i < Application.OpenForms.Count; i++)
{
    if (Application.OpenForms[i].Visible == true)
        count++;
}

So, you can modify your code to:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    int count = 0;
    for (int i = 0; i < Application.OpenForms.Count; i++)
    {
        if (Application.OpenForms[i].Visible == true)
            count++;
    }
    if (count > 1)
    {
        if (!YesNoBox.Show("Title", "Message", this))
        {
            e.Cancel = true;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM