Multiple Forms in C# [SOLVED]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Multiple Forms in C# [SOLVED]

Post by mattykins »

So i'm working in Windows Forms C# and in a program i'm making there is multiple forms. How would i get it so when i press a button, one form hides and another shows? So there is only 1 form showing at a time. Thanks in advanced! :D
Last edited by mattykins on Sat Mar 10, 2012 5:34 am, edited 1 time in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Multiple Forms in C#

Post by Jackolantern »

In C#, forms are handled just as any other object is. You first instantiate a new form, and then call the form.show() method to show it in a non-modal way, or form.showDialog to show it in a modal fashion. You can then call form.hide() on the old form to hide it. Or you can use form.close() to actually remove it from memory. Here is the MSDN entry for the Form class, which has a full listing of the properties and methods you can use in your applications on the Form class.
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Multiple Forms in C#

Post by mattykins »

I had tried that, when I click the start button it runs this code

Code: Select all

formCharCre f = new formCharCre();
            f.Show();
            Titlescreen t = new Titlescreen();
            t.Hide();
However it still doesn't hide the Titlescreen form.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Multiple Forms in C#

Post by Jackolantern »

Because you just created a new TitleScreen and hid it without it ever being shown. Instead, inside the TitleScreen class, try:

Code: Select all

this.hide();
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Multiple Forms in C#

Post by mattykins »

Thanks, worked great :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Multiple Forms in C# [SOLVED]

Post by Jackolantern »

Awesome! You're most welcome :)
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”