Cant access instance of one form on another

cheezy

Newcomer
Joined
Apr 11, 2003
Messages
13
Location
Tulsa, Oklahoma
Hi:

I have instances of two forms open; how can I close ('dispose' of) one instance from the other?

Specifically I have the following: Class Forms A,B and C. Form_A has the 'entry point' to the application with a button that opens an instance of form_B (called 'instance_B'). Form B as well has a button creating an instance of form_C (called 'instance_C') which I would LIKE to have a button on it disposing of the form B instance.

Is this possible? I can provide example code to make my question more specific if somebody would like.

Thanks.
 
I'm sorry; I tried something like this without success. I've tried constructing form B with a reference to form A:

form B(form_A A)

with a button whose click event is basically trying to dispose of the instance of form A, but that doesn't work.

Can you give me an example?
 
You dont create a new instance from what i see.
Try something like this:
C#:
form B = new form(form_A A);
 
So I looked at overloading constructors and creating a constructor for form B to accept the passing of the reference to an already-created instance of form A:

Form A info:

public class Form_A : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;

public Form_A()
{
InitializeComponent();
}
(other stuff)

static void Main()
{
Form_A instance_A= new Form_A();
instance_A.Show();
Application.Run();
}

private void button1_Click(object sender, System.EventArgs e)
{
Form_B instance_B= new Form_B(instance_A);
instance_B.Show();
}

} //end of class Form_A

//Form_B starts here
public class Form_B : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

public Form_B()
{
InitializeComponent();
}
public Form_B(Form_A Joe)
{
InitializeComponent();
}
(other stuff)
} //end of class Form_B

So I am creating an instance of Form_A (called instance_A) and I'm trying to pass that object (by reference) to the (overloaded)constructor of Form_B, but I get an error in Debugging where I am trying to create instance_B:

ERROR: D:\visualstudio\projects\TwoFormTest1\Form_A.cs(94): The name 'instance_A' does not exist in the class or namespace 'TwoFormTest1.Form_A'

Is my syntax wrong? Is the fact that Form_B has no Main()? Anything else? Again, I'm new at this and would appreciate any comments, with thanks in advance.
 
static void Main()
{
Form_A instance_A= new Form_A();
instance_A.Show();
Application.Run();
}

You get that error because instance_A is only known in the block it is declared in- (Main).

Try this:

Declare instance_A inside only the class block making it a global variable:

Form_A instance_A;
You may need to make it null. EX: Form_A instance_A=null;

Then in Main:
instance_A= new Form_A();


Always remember this: variables declared in a block are only known in that block unless they are declared static.
 
Back
Top