Solved

C# - Calling to Form2 from Form1

Posted on 2009-05-14
15
2,341 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
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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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 85

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 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