Ok. I have three forms. What I would like to know is when I make an instance of Form1, how to make it accessible from any form without having to create another instance.
If you don't want to go down the MDI route, you'll need a 'form manager' of some sort. Just create a static class that stores a form collection that you can Add() to etc.
No I tried making it public. That didnt work. Let me try and rephrase my question.
what I would like to know is how to make an instance of a form accessible from ALL forms. For example:
I have 3 forms.
namespace WindowsApp1 { public partial class Form1 : Form { Form1 form = new Form1();
public Form1() { InitializeComponent(); }
This is me creating an instanced of Form1 in the Form1's code. Now what I want to know is how to make that form and its objects accessible from the other forms. For example:
namespace WindowsApp1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); }
In order for your textbox to be visible in other classes you have to make them public too.. if you use the VS designer edit the Form1.designer.cs and change the private System.Windows.TextBox textBox1;to public System.Windows.Forms.TextBox textBox1;
But i dont understnad why not makeing in the same form class that u are working on another instance of Form1 there are just 4 words..
Create the form1 as a singleton object and let the other objects call the sinfleton class which will always return an the same object if the object is present or incase it is not then create a new one.
Form Instances
_Michael Fischer_
If you don't want to go down the MDI route, you'll need a 'form manager' of some sort. Just create a static class that stores a form collection that you can Add() to etc.
HTH
Aran
myForm.Show();
If you have Form1 open and (maybe hidden, whatever).. the above will show the original instance of Form1.
Mungos
No I tried making it public. That didnt work. Let me try and rephrase my question.
what I would like to know is how to make an instance of a form accessible from ALL forms. For example:
I have 3 forms.
namespace WindowsApp1
{
public partial class Form1 : Form
{
Form1 form = new Form1();
public Form1()
{
InitializeComponent();
}
This is me creating an instanced of Form1 in the Form1's code. Now what I want to know is how to make that form and its objects accessible from the other forms. For example:
namespace WindowsApp1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
form.textBox1.Text = "hello";
}
vka
how about makeing it public
Pdoh
if you use the VS designer edit the Form1.designer.cs and change the private System.Windows.TextBox textBox1; to public System.Windows.Forms.TextBox textBox1;
But i dont understnad why not makeing in the same form class that u are working on another instance of Form1 there are just 4 words..
WtBck
Hi,
you can use the Singleton design pattern ;).
Create the form1 as a singleton object and let the other objects call the sinfleton class which will always return an the same object if the object is present or incase it is not then create a new one.
Regards,
Nasha