How to access the public methods of existing Windows Form

From one Windows Form class, how do I access a public method of an existing instance of another Windows Form class in the same Project  ( Said existing Instance not having been created within the first mentioned class). 

Thanks in advance.



Answer this question

How to access the public methods of existing Windows Form

  • CSIROSwan

    Give the man a cigar!! Solved my problem. Thanks very much!
  • Knightendo

    cool. . .

    Thought about this after I posted.

    you might want to apply a factory pattern on that . . .

    hide the constructor, sync the instance. . . no need for the events. Read up on Static constructors and Factory Pattern:

    ==================
    public partial class MyForm : Form
    {
        static MyForm _instance;

        private static _sync; 
        static MyForm(){_sync = new object();}
        private MyForm(){}

        public static MyForm Instance
        {
            get
            {
                lock(_sync)
                {
                    if (_instance == null)
                        _instance = new MyForm();
                }
                return _instance;
            }
        } 

        protected override void OnClosed(EventArgs e)
        {
          _instance = null;
          base.OnClosed(e);
        }

    }

    ==================

    hmmm. . . does the close need to be synched

    do either need to be synched

     



  • AGW

    well. . .is this a singleton form. . . meaning that the application can only have one instance of the form available to it

    if so. . .

    ==================

      public partial class MyForm : Form
      {
        static MyForm _instance;

        public static MyForm Instance
        {
          get { return MyForm._instance; }
        }

        public MyForm()
        {
          InitializeComponent();
          _instance = this;
        }

        protected override void OnLoad(EventArgs e)
        {
          if (MyFormLoaded != null) MyFormLoaded(this, EventArgs.Empty);
          base.OnLoad(e);
        }
        protected override void OnClosed(EventArgs e)
        {
          _instance = null;
          if (MyFormClosed != null) MyFormClosed(this, EventArgs.Empty);
          base.OnClosed(e);
        }

        public static event EventHandler MyFormLoaded;
        public static event EventHandler MyFormClosed;

      }

    ==================

    you can refer to the instance via MyForm.Instance. . .

    now the reason for the static events. . .

    lets say you have an openwindow menu item on the main form. . .well then

    ==================

      public partial class MainForm : Form
      {
        public MainForm()
        {
          InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          MyForm.MyFormLoaded += new EventHandler(MyForm_Loaded);
          MyForm.MyFormClosed += new EventHandler(MyForm_Closed);

        }

        void MyForm_Loaded(object sender, EventArgs e)
        {
          openFormToolStripMenuItem.Enabled = false;
        }

        void MyForm_Closed(object sender, EventArgs e)
        {
          openFormToolStripMenuItem.Enabled = true;
        }

        private void openFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
          new MyForm().Show();
        }
      }

    ==================

    Make sense

     



  • How to access the public methods of existing Windows Form