1. How do i close 2 forms in runtime.

Eg. I click BTNCANCEL in frm1 it will open frm2 and i want to click BTNNO in frm2 to close all forms in the runtime. How do i go about doing that.

[frm2 is just asking the user whether to save the data that have been change if NO CLOSE ALL APPLICATION.]

2. Save Button

I'm currently doing a config. file and I need to click BTNSAVE in frm1 to save all details that i have enter into a textfile so that other application can retrieve the settings.


In frm1, i have 3 combo box[cmbStart, cmbEnd, cmbintervals and 5 text boxes[tbpassword,tbnewpass,tbconfirmpass,tbimageloc,tbdatabaseloc.

If you want to close all the forms, you can write this line in the event handler of the BTNNO click event handler

Application.Exit();

But if you need to close some forms, you should declare them in the form opens them globally then call it like that formInstance.Close(); this also in the BTNNO event handler and sure this button in the form opens the rest of forms (LOOL I hope you get me right now).
I think you should reopen the forms again to be affected.

Ooo I see. Thanks=)

As in when I want to close only certain forms then I'll need to declare
frmInstance.Close(); in the event handler of BTNNO? [haas a little blur]

when eg. frm 2 is open?

hmmm, look
If you have button open in Form1 to open Form2, then you should declare Form2 as global variable in Form1 to be able to play with it.

//Form 1 class
class Form1 : Form
{
Form2 m_form2;

//Click event handler of Open button
public void OpenClick(object sender, EventArgs e)
{
m_form2 = new Form2();
m_form2.Show();
}

//Click event handler of Close button
public void CloseClick(object sender, EventArgs e)
{
//be sure it initialized
m_form2.Close();
}
}

That's it :)

1. How do i close 2 forms in runtime.

Eg. I click BTNCANCEL in frm1 it will open frm2 and i want to click BTNNO in frm2 to close all forms in the runtime. How do i go about doing that.

[frm2 is just asking the user whether to save the data that have been change if NO CLOSE ALL APPLICATION.]

2. Save Button

I'm currently doing a config. file and I need to click BTNSAVE in frm1 to save all details that i have enter into a textfile so that other application can retrieve the settings.


In frm1, i have 3 combo box[cmbStart, cmbEnd, cmbintervals and 5 text boxes[tbpassword,tbnewpass,tbconfirmpass,tbimageloc,tbdatabaseloc.

REPLY:

I guess U cannot close two forms in Runtime. but u can save n number of forms at a time.

OnButton click you can do that. Let me know if it is work out for u 1

commented: Don't give wrong answers! -2

You can close any number of forms you want at runtime. The only limitation is how you choose to go about it. Explain the call order for these forms. Here is one way to go about it:

namespace daniweb
{
  public partial class frmHTTP : Form
  {
    private frm1 _frm1;
    private frm2 _frm2;

    public frmHTTP()
    {
      InitializeComponent();
    }

    private void frmHTTP_Load(object sender, EventArgs e)
    {
      _frm1 = new frm1();
      _frm2 = new frm2();
      _frm1.Show();
      _frm2.Show();
    }


    private void button3_Click(object sender, EventArgs e)
    {
      CloseForm(_frm1);
      _frm1 = null;
      CloseForm(_frm2);
      _frm2 = null;
    }

    private static void CloseForm(Form f)
    {
      if (f == null)
        throw new ArgumentNullException("f");
      f.Close();
      f.Dispose();
    }

Technically you only need to call close on a form shown as non-modal and the CLR will dispose it and garbage collect it, but it helps to explicitly dispose IDisposable objects whenever possible.

heyy=) actually the previous code you give me work because i wanted close all application hee. didn't thought of that.=) thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.