473,416 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

How to access Form2 from Form1

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
10 12360
Bassem
344 100+
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
jg007
283 100+
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
jg007
283 100+
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
@sbandalli
Thanks a lot........It worked.....:)
Feb 5 '09 #5
@jg007
Thanks a lot ...It worked...:)
Feb 5 '09 #6
r035198x
13,262 8TB
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
Bassem
344 100+
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
jg007
283 100+
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
jg007
283 100+
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
r035198x
13,262 8TB
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

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

Similar topics

1
by: HKSHK | last post by:
Hello all, I have this problem: I have 2 forms. Form1 contains a checkbox, which I want to uncheck from Form2. I set the checkbox to be "public static" and when I try to change a property...
5
by: Lucas Graf | last post by:
I feel like a bonehead because this seems so easy. I have 2 forms in the same namespace, Form1 and Form2. Form1 is a MDI Container. Form1 has a status bar : statusbar1 Form1 has a basic menu...
5
by: nadir b | last post by:
hi I don't know how to change for exemple a form1 caption text from form2 don't forget that form2 has created from form1 I want sample code with c# *** Sent via Developersdex...
4
by: Carlos | last post by:
In VB6 I was able to accessa control like a progressbar form a different form, for example frm1.progressbar1.value =XXX but now how can I do that in VB.NET Thanks
5
by: PAPutzback | last post by:
Form2 has one purpose to open and list some names and ids. I want to handle the list box click event on form2 so I can get the selected value onto a field in form1. I changed this Dim MyForm2...
5
by: Boki | last post by:
Hi All, There are two forms, when some click happen, it fires to set form2's viable as enable. The form2 is for user to input some text and then the data need to be collected into form1's...
7
by: Boki | last post by:
Hi All, I want to change WindowState of form1 from form2. I tried these two methods, but no luck on both. (1) Declare a public method: /* function of form1 */ public void...
9
by: Anil Gupte | last post by:
I have a MDI Parent Child set of forms FormContainer --MDIParent FormStart --MDIChild FormMain-->MDIChild FormSliceInfo-->MDIChild I use the following in the beginning of FomrContainer ...
5
by: lukasmazur | last post by:
Hi I have a problem with using listBox1. I have a two forms form1 and form2. In form1 are controls listBox1, textBox1 and button witch creating object of class Form2. In class Form2 I create a...
1
by: c2015 | last post by:
I need help with some VB code. I will keep it generic and clear as possible. I have two forms lets call Form1 and Form2 I have a class module (CM) and a sub class module (SCM) instantiated in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.