Click here to Skip to main content
15,303,522 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a simple button in form1, I'm having a hard time making it that when you click on the button in form1 form1 will change into form2. The only thing I can do is make form2 open on a new form. I forgot how to do this and I need someone to help me jog my memory!

Updated 10-Jul-12 4:26am
[no name] 10-Jul-12 10:27am
You mean: frm2.Show(); frm1.Hide(); ?
Sergey Alexandrovich Kryukov 10-Jul-12 15:45pm
What, you forgot how to make the impossible? Wow, please remember it as soon as you can, I want to know your trick. :-)
MR. AngelMendez 11-Jul-12 9:00am
yeah its an old hacker trick that was passed down to me but cant remember lol :)
Sergey Alexandrovich Kryukov 11-Jul-12 14:18pm

1 solution

You can't actually change Form1 into Form2 - they are normally separate classes and can't be "mutated" from one into the other.
There are a couple of ways you can get that visual effect though.

The first is to make them the same size and shape, and set the new form StartPosition to Manual. You then set the Loctation property so that it matches the Form1. You can then Show the Form2, and Hide Form1 - do not try to use ShowDialog on Form2 unless you Hide Form1 first! If you do this, you must provide a way to return to Form1, or a different way to terminate the application - closing Form2 will not do that. One way is to handle the Form2 FormClosing event, and either Close Form1, or re-show it.

The other way is to put the whole of your form into a UserControl and display that on the Form surface. When you need to swap to "Form2" you just make the "Form1" UserControl Visible property false, and the "Form2" one true. Like magic, the form appears to change!

Personally, I tend to use the latter method.
MR. AngelMendez 10-Jul-12 11:23am
oooh user controls I forgot about those, never really could figure out how to display them on my form though. I'll need help with this.

OriginalGriff 10-Jul-12 12:03pm
Easy Peasy.
Create your controls and build your project (you haven't added them to your form yet).
You should find the controls appear in the toolbox.

If they don't, right click on the Toolbox, and select "Choose Items".
On the dialog that eventually appears, press the "Browse" button, and locate the DLL or EXE with the controls in.

You can now drag the controls onto your form.
MR. AngelMendez 11-Jul-12 9:07am
thank you so much OriginalGriff, I got it to work!!
OriginalGriff 11-Jul-12 9:29am
You're welcome!
Sander Rossel 10-Jul-12 11:29am
UserControls definitely get my 5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900