Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am uday satardekar,

In my MDI application in csharp winform there is 5 child form.

And I want only one form of each child form after click.

Which work properly for me. I have achieved this using static counter .


When user click next after opening each child form, I want clicked instance in front.


My code is like this:

C#
private void retriveDataToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            int instances = RetriveDataForm.instanceCount;
            if (instances < 1)
            {
                RetriveDataForm retrieveDataForm = new RetriveDataForm();
                retrieveDataForm.MdiParent = this;
                retrieveDataForm.Show();
            }

        }

        private void manualEntryToolStripMenuItem1_Click(object sender, EventArgs e)
        {
             int instances = ManualEntryForm.instanceCount;
             if (instances < 1)
             {
                 ManualEntryForm manualEntry = new ManualEntryForm();
                 manualEntry.MdiParent = this;
                 manualEntry.Show();
             }
        }

Suppose retrieveDataForm and manualEntry are already opened and when user click next time on manualEntry form, I want it front of all opened form.

I have use manualEntry.BringToFront(); in else block which is not working.
Please give me solution.
Posted
Updated 14-Aug-11 23:38pm
v2

Regarding this,
SQL
use manualEntry.Activate();

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activate.aspx[^]



i have use like this

C#
int instances = StatusForm.instanceCount;
          int instances = ManualEntryForm.instanceCount;
             if (instances < 1)
             {
                 ManualEntryForm manualEntry = new ManualEntryForm();
                 manualEntry.MdiParent = this;
                 manualEntry.Show();
             }
           else
           {

               manualEntry .Activate();
             
           }



but it is not working properly
please give me solution


thanks
 
Share this answer
 
You don't need to count anything. Just enumerate the existing opened forms, and if you find manualEntry, call it's Activate method. Otherwise, create it.

C#
foreach (Form child in mainForm.Children)
{
    if (child is manuEntry)
    {
        child.Activate();
        break;
    }
}
if (!(this.ActiveMdiChild is manualEntry))
{
    manualEntry form = new manuEntry();
    form.Show();
    form.Focus();
}
 
Share this answer
 
v2
Comments
udusat13 15-Aug-11 6:13am    
But please tell me what mainForm.Children

in foreach (Form child in mainForm.Children)
thanks.
I think John meant mainForm.MdiChildren
 
Share this answer
 
Comments
udusat13 15-Aug-11 6:31am    
please help me sir i am getting confused
#realJSOP 15-Aug-11 7:49am    
Yes, I did. That's what I get for being in a hurry (to leave for work). :)
Ok, the below code should work for you, assuming ManualEntryForm is the name of one of your Forms and the code is placed in the MDI parent. I don't know how else I can be of help.

C#
foreach (Form child in MdiChildren)
{
    if (child is ManualEntryForm)
    {
        child.Activate();
        break;
    }
}
if (!(this.ActiveMdiChild is ManualEntryForm ))
{
    ManualEntryForm form = new ManualEntryForm { MdiParent = this };
    form.Show();
    form.Focus();
}
 
Share this answer
 
Comments
udusat13 15-Aug-11 7:05am    
Thank u sir,
Its really working for me.
Thanksssssssssssssssssssssssssssssssssssssssssss!
Simon Bang Terkildsen 15-Aug-11 7:15am    
close the question please, accept an answer :)
Simon Bang Terkildsen 15-Aug-11 7:09am    
You're welcome
 
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