Solved

C# - Calling to Form2 from Form1

Posted on 2009-05-14
15
2,357 Views
Last Modified: 2013-12-17
Hello,

I have the following code. I am calling to Form2 from Form1.
At Form2 code I need to open Form1 back again: form1.show();
and to use myFlag parameter.

But form1 is not defined in Form2 class. It is just an object that called Form2.
How to do it?

THANKS
Margarit
public partial class Form1: Form
    {
              Form2 form2 = new Form2();
               public int myFlag;
              public Form1()
             {
                     form2.Show();
                     InitializeComponent();
             }

Open in new window

0
Comment
Question by:margarit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24383499
You could mark your myFlag as static and it would then be class-level instead of instance-level. Then use form2.ShowDialog() as such:
public partial class Form1: Form
{
    Form2 form2 = new Form2();
    public static int myFlag;
    
    public Form1()
    {
        form2.ShowDialog();
        InitializeComponent();
    }
    
    
/////////////////////////////////////
// Within Form2 code
/////////////////////////////////////
public partial class Form2: Form
{
 
    public void SomeFunction()
    {
        Form1.myFlag = 100;  // Example of interacting with static variable; Use ClassName.VariableName to access
    }

Open in new window

0
 

Author Comment

by:margarit
ID: 24383551
THANKS!!!!
It helped!
Another question:
How can I show Form1 from Form2. To go back to Form1.
THANKS
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24383576
Or you could pass the instance of form1 into the constructor of form2 and keep a reference there like this:
public partial class Form1: Form
{
    Form2 form2 = new Form2(this);
    public int myFlag;
 
    public Form1()
    {
        form2.ShowDialog();
        InitializeComponent();
    }
    
    
/////////////////////////////////////
// Within Form2 code
/////////////////////////////////////
public partial class Form2: Form
{
    private form1Instance;
 
    public Form2(Form1 myForm1)
    {
        form1Instance = myForm1;
        form1Instance .myFlag = 100;  // Example of interacting with public variable;
    }
}

Open in new window

0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:margarit
ID: 24383578
Hello,
I used form2.ShowDialog() . But inside form2 there is no Form1.Show() option
0
 

Author Comment

by:margarit
ID: 24383620
In a case I need to go back from form1 to form2. how to do it?
There is no option form1.Show();
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24383663
you need to create an instance of the form to use the .Show() method
Have a look at my example earlier of passing the instance of Form1 to Form2
Although as you are showing form2 as a modal dialog you will can only show a new instance of Form1 not the original one. To be able to show the original one you need to change the call to show form2 to
form2.Show();
 
0
 

Author Comment

by:margarit
ID: 24383737
private form1Instance;
Should it be:
 private Form form1Instance;


 
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24383752
sorry yeah shouldbe
private Form1 form1Instance;
0
 

Author Comment

by:margarit
ID: 24383778
THANKS :)
Form2 form2 = new Form2(this);
I get an error that this is not allowed here
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24383792
did you change the constructor in form2 to take a parameter of the type Form1 like this:
    public Form2(Form1 myForm1)
    {
        form1Instance = myForm1;
        form1Instance .myFlag = 100;  // Example of interacting with public variable;
    }

Open in new window

0
 

Author Comment

by:margarit
ID: 24383812
yes, I did
0
 
LVL 12

Accepted Solution

by:
GuitarRich earned 500 total points
ID: 24383839
ah sorry I see the issue - change it to this - I have change where you initialise the form2 object to within the form1 constructor.
public partial class Form1: Form
{
    Form2 form2;
    public int myFlag;
 
    public Form1()
    {
        form2 = new Form2(this);
        form2.ShowDialog();
        InitializeComponent();
    }
    
    
/////////////////////////////////////
// Within Form2 code
/////////////////////////////////////
public partial class Form2: Form
{
    private form1Instance;
 
    public Form2(Form1 myForm1)
    {
        form1Instance = myForm1;
        form1Instance .myFlag = 100;  // Example of interacting with public variable;
    }
}

Open in new window

0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24383923
You can use ShowDialog,

Form2 frm = new Form2();
frm.ShowDialog(this);

You can mention ShowDialog(this), this defines your Form 1 is the owner of Form 2.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24387675
If you are showing Form2 via ShowDialog() then all you need to do is close Form2 to get back to Form1...

    // in Form2
    this.Close() // <--- code will now RETURN to Form1 after the ShowDialog() call!

...since execution in Form1 STOPS until your instance of Form2 is closed.
0
 

Author Closing Comment

by:margarit
ID: 31581379
THANKS
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question