Form Instances

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.


Answer this question

Form Instances

  • wattgn

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


  • albedo20

    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";

    }



  • Kriske

    Form1 myForm = (Form1)Application.OpenForms["Form1"];

    myForm.Show();

     

    If you have Form1 open and (maybe hidden, whatever).. the above will show the original instance of Form1.

     



  • DbHd

    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



  • jeremy2006

    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



  • sjkowal

    hi

    how about makeing it public


  • Form Instances