Form switching

Program won't exit when closing on form2.

Page 1 of 1

14 Replies - 1290 Views - Last Post: 11 October 2010 - 02:21 PM Rate Topic: ***-- 2 Votes

#1 Gamma Ray   User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 144
  • Joined: 24-September 10

Form switching

Posted 11 October 2010 - 10:46 AM

Hey, I'm switching between two forms and when I switch to the second one, click on the [X] button, the window closes. But nothing else happens, the program is still running.

I have tried this:

1. I tried Close() instead of Hide(), but that didn't work.
2. I tried to find a way to close the first form when the second form gets closed, but didn't find a way to do that.
3. I ate a chocolate and drank milk in the hope to C# God. (Did you get it? xD ... See sharp god... Did I just ruin it? o_O)

Well, thanks in advance, here is the code I'm using:

            this.Hide();
            new FormCC().Show();


:)

This post has been edited by Gamma Ray: 11 October 2010 - 10:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Form switching

#2 Robin19   User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Form switching

Posted 11 October 2010 - 11:14 AM

I assume "this" is the first form you want to go back to. That form is still on the stack, you just need it to reverse Hide(). Try this:
this.Hide();
new FormCC().ShowDialog();
this.Show();

The ShowDialog() method does two things. It shows the form you call it on. It also takes control away from the form it is called from. It also resumes the function it is called in when form is closed. If you just called Show, the user can access both forms.

When you open up a "properties" box in another form, sometimes you can't access the original form. ShowDialog was called. In some programs you open a "properties" box and can still access the original form. Show was called.
Was This Post Helpful? 0
  • +
  • -

#3 Gamma Ray   User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 144
  • Joined: 24-September 10

Re: Form switching

Posted 11 October 2010 - 11:22 AM

Thanks, because what you said works.
I'll be using that as a temporary solution.

But what I want is to close all the forms when closing 'this'.
How do I do that? :)

This post has been edited by Gamma Ray: 11 October 2010 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5105
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Form switching

Posted 11 October 2010 - 11:59 AM

Application.Exit() should work.
Was This Post Helpful? 0
  • +
  • -

#5 Gamma Ray   User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 144
  • Joined: 24-September 10

Re: Form switching

Posted 11 October 2010 - 12:13 PM

Well, the problem is... Where do I add that? :/
Was This Post Helpful? 0
  • +
  • -

#6 Robin19   User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Form switching

Posted 11 October 2010 - 12:45 PM

On any form that you want. If FormCC wants to stop everything, don't call this.Close(). Use Application.Exit() instead. This will override all of the passing of control. Everything on the stack caused by your program will leave the stack.

The other way to do it piggy backs off of my earlier post. Instead of calling this.Show(), use this.Close(). Or use Application there. I like this.Close, since I don't want Form A closing Form B. It feels more OOP that way.

Edit: Look at Program.cs. There is a Main there that calls Application.Run. Application.Close goes back to this object to close everything that was created by Application.

This post has been edited by Robin19: 11 October 2010 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Form switching

Posted 11 October 2010 - 12:52 PM

Set the .Owner of the new form, to be assigned the previous.
In vb.net it something like
Dim nf As New SecondForm
nf.Owner=Me
nf.ShowDialog()



Now when the First Form closes it automatically closes, all of the forms it is the owner of.

This post has been edited by AdamSpeight2008: 11 October 2010 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#8 Gamma Ray   User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 144
  • Joined: 24-September 10

Re: Form switching

Posted 11 October 2010 - 01:04 PM

Yes, but like... How do I make it like:

if (FormCCisClosing)
{
    Application.Exit();
}

(the code above is made to explain my problem...)

I can't find out where to write Application.Exit.

This post has been edited by Gamma Ray: 11 October 2010 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades   User is online

  • Saucy!
  • member icon

Reputation: 6248
  • View blog
  • Posts: 24,015
  • Joined: 23-August 08

Re: Form switching

Posted 11 October 2010 - 01:04 PM

I still think the most obvious answer has yet to appear.

Every Form has associated with it a FormClosed event. Create the instance of the second form, attach an event handler to the its FormClosed event and hide the first form. Inside the event handler call either Application.Exit() or this.Close() to close the main form.

EDIT: Part of a tutorial group I'm working on:

private void button1_Click(object sender, EventArgs e)
{
    CreateAccountForm caf = new CreateAccountForm();
    caf.FormClosed += new FormClosedEventHandler(caf_FormClosed);
    this.Hide();
    caf.ShowDialog();
}

void caf_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Close();
}

This post has been edited by JackOfAllTrades: 11 October 2010 - 01:06 PM

Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Form switching

Posted 11 October 2010 - 01:07 PM

You could swap the owner, so the new new form is the owner of the previous.

Edit
It's not owner I thinking of but
nf.AddOwnedForm(Me)


This post has been edited by AdamSpeight2008: 11 October 2010 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

#11 Gamma Ray   User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 144
  • Joined: 24-September 10

Re: Form switching

Posted 11 October 2010 - 01:07 PM

Thanks, JackOfAllTrades! :)

I'll look at that.
Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Form switching

Posted 11 October 2010 - 01:20 PM

Also try.
private void button1_Click(object sender, EventArgs e)
{
    CreateAccountForm caf = new CreateAccountForm();
    caf.AddOwnedForm(this);
    this.Hide();
    caf.ShowDialog();
}


Was This Post Helpful? 1
  • +
  • -

#13 JackOfAllTrades   User is online

  • Saucy!
  • member icon

Reputation: 6248
  • View blog
  • Posts: 24,015
  • Joined: 23-August 08

Re: Form switching

Posted 11 October 2010 - 02:03 PM

Excellent Adam, I learned something new as well!
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Form switching

Posted 11 October 2010 - 02:16 PM

It's used so you can have floating tool-bars, associated to a form, so when that form closes it closes to tool-bars.
Was This Post Helpful? 0
  • +
  • -

#15 Gamma Ray   User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 144
  • Joined: 24-September 10

Re: Form switching

Posted 11 October 2010 - 02:21 PM

I'll look on it later, thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1