• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 753
  • Last Modified:

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

0
johny_bravo1
Asked:
johny_bravo1
  • 6
  • 3
1 Solution
 
CuteBugCommented:
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.
0
 
johny_bravo1Author Commented:
"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.
0
 
johny_bravo1Author Commented:
this line objMDI.Focus();
is actually f2.Focus();
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
johny_bravo1Author Commented:
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.
0
 
käµfm³d 👽Commented:
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

0
 
johny_bravo1Author Commented:
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.
0
 
johny_bravo1Author Commented:
what do you mean by "first_instance_of_form"
should I set nextForm = new MDIForm();

I am just confused.Please guide me.
0
 
käµfm³d 👽Commented:
First_instance_of_form refers to whichever MDI you open first--store that to the nextForm to initialize nextForm. Then, when you call the form2.Show(), your nextForm will point to the original form, but form2 will be visible. Then, when you call BLTools.nextForm.Show() et al, form1 will be shown and form2 will be stored for the next switch.

I assumed since you were keeping track of mdicount in the BLTools class, keeping track of the "hidden" form wouldn't be that big of a deal. If this will violate your BL layer, then perhaps we can figure a different method.
public class BLTools
{
    public static int mdicnt = 0;
    public static Form nextForm;
}
 
public class MDIForm : Form
{
    public MDIForm()
    {
        InitializeComponent();
 
        if (BLTools.mdicnt == 0)
        {
            BLTools.nextForm = this;                
        }
 
        BLTools.mdicnt++;
    }
 
    private void multiSessionToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (BLTools.mdicnt == 2)
        {
            MessageBox.Show("You can have only two instances running at a time");
        }
        else
        {
            MDIForm f2 = new MDIForm();
            f2.Show();
            BLTools.mdicnt++;
        }
    }
 
    private void switchToolStripMenuItem_Click(object sender, EventArgs e)
    {
        BLTools.nextForm.Show();
        BLTools.nextForm = this;
    }

Open in new window

0
 
johny_bravo1Author Commented:
Thanks a lott
0
 
käµfm³d 👽Commented:
NP :)
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now