Solved

C# - Calling to Form2 from Form1

Posted on 2009-05-14
15
2,377 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

630 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