Link to home
Start Free TrialLog in
Avatar of Johny Bravo
Johny Bravo

asked on

Switching between forms

Hi Experts,
I am developing a Windows application.
I have a MenuStrip which is having two menu as 'MultiSession' and 'Switch'.
Click on Multisession will create a new instance of the for.And Switch should be used for switching between forms.
(These are two different MDI forms)
I am setting variables in class file named 'BLTools '.
private void multiSessionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (BLTools.mdirunn == 1)
            {
                MessageBox.Show("You can have only two instances running at a time");
            }
            if (BLTools.mdicnt != 1)
            {
                MDIForm f2 = new MDIForm();
                f2.Show();
                BLTools.mdicnt = 1;
                BLTools.mdirunn = 1;
            }
          
        }
 
//Switch menu
 private void switchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //What code should be here
        }

Open in new window

Avatar of CuteBug
CuteBug
Flag of India image

Make f2 as the member of your class and instantiate it in multiSessionToolStripMenuItem_Click().

In the switchToolStripMenuItem_Click() method

you can switch to f2 by calling the Focus method.

f2.Focus(); // To switch to f2.

Similarly call the Focus for the other form to switch to it.
Avatar of Johny Bravo
Johny Bravo

ASKER

"Make f2 as the member of your class "

By this what I get is,
I declared MDIForm f2  in class,and now

 private void multiSessionToolStripMenuItem_Click(object sender, EventArgs e)
        {
         
            if (BLTools.mdirunn == 1)
            {
                MessageBox.Show("You can have only two instances running at a time");
            }
            if (BLTools.mdicnt != 1)
            {
                BLTools.mdicnt = 1;
                BLTools.mdirunn = 1;
                f2 = new MDIForm();
                f2.Show();
                               
            }
         
        }
 private void switchToolStripMenuItem_Click(object sender, EventArgs e)
        {
         
            objMDI.Focus();
        }


This is not working.I am sure I didn't get you right,pls explain.
this line objMDI.Focus();
is actually f2.Focus();
Ok
I have to make one poine here.
MDIForm.Show will create a New MDIForm.
First MDIForm is also running simultaneouly.
These are two different MDIForms.
Avatar of kaufmed
In your BLTools class, keep a reference to the next displayed form. When the user clicks "Switch," the class shows the next form and updates the reference to the previously visible form:
// In BLTools
 
protected static Form nextForm;
 
nextForm = first_instance_of_form;
 
// In Menustrip code
 
private void switchToolStripMenuItem_Click(object sender, EventArgs e)
{
    BLTools.nextForm.Show();
    BLTools.nextForm = this;
}

Open in new window

Hi kaufmed,
thanks for your comment.
BLTools is in business layer so can't get form reference thee.

Further,on click on Switch on any of the two MDIForms,the other form should be visible.

Please help.
what do you mean by "first_instance_of_form"
should I set nextForm = new MDIForm();

I am just confused.Please guide me.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lott
NP :)