user2327043
user2327043

Reputation: 89

How to make first form disabled if second form is open?

I have used this code to open a new form from the current form :

 private void add_Click(object sender, EventArgs e)
 {
      add obj = new add();
      obj.Show();
 }

 private void welcome_FormClosed(object sender, FormClosedEventArgs e)
 {
      Application.Exit();
 }

 private void view_Click(object sender, EventArgs e)
 {
      view obj = new view();
      obj.Show();
 }

 private void update_Click(object sender, EventArgs e)
 {
      update obj = new update();
      obj.Show();
 }

 private void delete_Click(object sender, EventArgs e)
 {
      delete obj = new delete();
      obj.Show();
 }

In this if i open any form, then the previous form also works as usual. I want that if a form is open then all other previous form get closed or disabled and sound like a beep on click event of previous button.

Upvotes: 2

Views: 9279

Answers (3)

VJain
VJain

Reputation: 1059

try to use, this will close your current form.

obj.ShowDialog();
 this.close();

Upvotes: 1

Edper
Edper

Reputation: 9322

Why not try Hiding your form and then Close it when the called form is Closed so that it will not stay in memory. Like this.

Let's say in Form1 you click a Button to show Form2

 Form2 frm2 = new Form2();
 frm2.Activated += new EventHandler(frm2_Activated);
 frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
 frm2.Show();

Now, this one is when the Form2 showed or is Activated you hide the calling form, in this case the Form1

    private void frm2_Activated(object sender, EventArgs e)
    {
        this.Hide(); // Hides Form1 but it is till in Memory
    }

This one when the Called form is Closed in this case Form2 it will also Close Form1 so that it will not stay in the memory.

   private void frm2_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close(); // Closes Form1 and remove this time from Memory
    }

Upvotes: 4

user1522991
user1522991

Reputation:

You have to use ShowDialog instead of Show.

ShowDialog():

Opens a window and returns only when the newly opened window is closed.

Show():

Opens a window and returns without waiting for the newly opened window to close.

Upvotes: 6

Related Questions