Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# - Calling to Form2 from Form1

Posted on 2009-05-14
15
Medium Priority
?
2,415 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

971 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