By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

How to access Form2 from Form1

P: 53
I am currently doing a project on winform in C#. I have created form1. I need to create Form2 and when I click a button in Form1 it should direct me to Form2. How do I do this?? If I have to create an reference of form2 in form2 , do I do this in the main() method? I really appreciate if someone can help me on this, since I am new to C#
Feb 5 '09 #1
Share this Question
Share on Google+
10 Replies


100+
P: 344
Hi,
Make a reference to form2, then make an event handler, create the form then show it, you can also hide form1.
form2 f2;
f2 = new form2 ( ) ;
f2.Show ( ) ;
this.Hide ( );
Try this and tell me if there is a problem.
Regards,
Bassem
Feb 5 '09 #2

100+
P: 283
waits to be shot down for lousy coding ....

I have been using this -

Form1

Expand|Select|Wrap|Line Numbers
  1.  
  2.     public partial class Form1 : Form
  3.     {
  4.         Form2 SecondForm = new Form2();
  5.         public static Form Mainform = null;
  6.  
  7.         public Form1()
  8.         {
  9.             InitializeComponent();
  10.         }
  11.  
  12.         private void button1_Click(object sender, EventArgs e)
  13.         {
  14.             Mainform = this;
  15.             SecondForm.Show();
  16.             this.Hide();
  17.  
  18.         }
  19.  
  20.      }
  21.  
  22.  
Form2

Expand|Select|Wrap|Line Numbers
  1.  
  2.     public partial class Form2 : Form
  3.     {
  4.         public Form2()
  5.         {
  6.             InitializeComponent();
  7.         }
  8.  
  9.         private void button1_Click(object sender, EventArgs e)
  10.         {
  11.             Form1.Mainform.Show();
  12.             this.Hide();
  13.         }
  14.     }
  15.  
  16.  
  17.  
Feb 5 '09 #3

100+
P: 283
if you keep on creating a new form you will create a new instance of the form so will lose data in and fields on the form and also end up using more memory and that is why I have declared the new form at the start and also created a reference on form1 ( mainform ) so that it could be shown again
Feb 5 '09 #4

P: 53
@sbandalli
Thanks a lot........It worked.....:)
Feb 5 '09 #5

P: 53
@jg007
Thanks a lot ...It worked...:)
Feb 5 '09 #6

10K+
P: 13,264
I see these types of questions here all the time. Unfortunately they arise due to improper design.
C# is an object oriented language not a Form oriented language. When creating a project in C#, the first step is to design your objects not your forms. Forms represent the view part of objects. You should not have Forms called form1,form2 e.t.c, rather you should have UserDetailsForm, InvoiceForm e.t.c. i.e the forms model objects.
What does that have to do with how to open one form from another?
Well the answer is that there are many ways of allowing form's controls to access, create or influence other forms or controls on other forms.

The correct way of doing it depends on what objects the forms are representing. e.g If they model a composition relationship i.e one of the objects being modeled is a member of the other object, then the correct way would be to create the member form as an instance variable of the containing form (as you did above). For other relationships, other techniques are needed.

The problem with hacking away at the keyboard providing handles to all forms everywhere just so the code works is that you create a huge bowl of spaghetti too difficult to digest in the near future when you shall surely need to add some seasoning to it. Just making a few additions later would create serious headaches on all the housekeeping issues that would be required. Usually people just go on and add other more ugly hacks when that happens so that the program works again e.t.c.

Better take a step back and do object oriented programming instead. It really is so much more simpler.

P.S I probably sound harsh and ranty but it's just I've been seeing these kinds of questions for far too long now.
Feb 6 '09 #7

100+
P: 344
Better take a step back and do object oriented programming instead. It really is so much more simpler.
I'm fully agree with.
Feb 6 '09 #8

100+
P: 283
sorry but I have to say that that is not the most helpful response Bassem, whilst I appreciate r035198x's comment and I will try to understand what he means please add something more than ' i agree '

I can fully accept that the code is not the best and it may be incorrect but examples and descriptions of why help more than just ' that is wrong '

this is not meant to be harsh but I tried to answer a question and while appreciate and accept corrections please do try to help me or others understanding not just increase your post count
Feb 6 '09 #9

100+
P: 283
Thanks Baseem , I am not fully sure what you mean but that may be due to my lack of understanding of object orientation but I appreciate you replying, if you have any links or tutorials to explain this further that would be great
Feb 6 '09 #10

10K+
P: 13,264
I did not say that jg007's response is wrong. I only pointed out that it's correctness is based on the objects involved and the relationships between them.
If composition is required then it is the common (correct) approach that is used.

P.S Bassem's post which I considered uneccesarily rude has now been removed.
@Bassem, you can contact me by PM if you thought that was not correct.
Feb 9 '09 #11

Post your reply

Sign in to post your reply or Sign up for a free account.