Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#Win7FormsWinForm
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!
 
Thanks.
Posted 10-Jul-12 4:24am
Edited 10-Jul-12 4:26am
v3
Comments
Wes Aday at 10-Jul-12 10:27am
   
You mean: frm2.Show(); frm1.Hide(); ?
Sergey Alexandrovich Kryukov at 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. :-) --SA
MR. AngelMendez at 11-Jul-12 9:00am
   
yeah its an old hacker trick that was passed down to me but cant remember lol :)
Sergey Alexandrovich Kryukov at 11-Jul-12 14:18pm
   
:-)

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
MR. AngelMendez at 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. thanks.
OriginalGriff at 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 at 11-Jul-12 9:07am
   
thank you so much OriginalGriff, I got it to work!!
OriginalGriff at 11-Jul-12 9:29am
   
You're welcome!
Naerling at 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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 950
1 OriginalGriff 508
2 Abhinav S 233
3 thatraja 225
4 Emre Ataseven 200
0 Sergey Alexandrovich Kryukov 8,142
1 OriginalGriff 4,665
2 Peter Leow 3,774
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 10 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid