Skip to content
Loading
  • Sourav Kumar Das
    Can you ellaborate it...at which part I should place that load function.
    @Amit Gupta
    +1
  • Amit Gupta
    Hi Rama Loop will only execute when it has any opened form. However, to achieve a task there aee many ways. He can apply any of the solution.
    +2
  • Amit Gupta
    Call that load method before opening a new form.
    +2
  • Sourav Kumar Das
    But I should not specify that this form should be closed at background whenever i open a new form at foreground...@Rama Krishna
    +1
  • Sourav Kumar Das
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
    Form2 frm = new Form2();
    frm.Show();
    load();
    }
    private void load()
    {
    foreach (Form f in Application.OpenForms)
    f.Close();
    }
     
    Like this way.....
     
     
    But it showing error...@Amit Gupta
     
    +1
  • Rama Krishna
    Hi amit,
     
    I think it's not better Idea because every time he has to loop and then hide Instead he could try what I provided 
    +3
  • Amit Gupta
    Create a function and write that code under that and whenever you are opening a form, just call that function.
    +2
  • Sourav Kumar Das
    foreach (Form f in Application.OpenForms)
    f Close();
     
    at where I should apply this one..@Amit Gupta
    +1
  • Rama Krishna
    Hi Sourav,
    1. First hide your current form
        this.Hide();
     
    2. Second initialize your new form
        NewForm form = new NewForm ();
     
    3. Third use below line
       form.Closed += (s, args) => this.Close();
     
    4. Finally use below line 
       form.Show();
     
    Entire code looks like below 
          this.Hide();
          NewForm form = new NewForm();
          form.Closed += (s, args) => this.Close();
          form.Show();
     
    +2
  • Amit Gupta
    Try this
    1. foreach (Form f in Application.OpenForms)
    2. f Close();  
     
    +2