Accessing main form objects in other .cs files

Viewed 6650

It's easy to access sub-window's objects from main file because we must create the sub window anyway. So there's something like this:

SubForm subform = new SubForm();
if (subform.ShowDialog() == DialogResult.OK)
{
    text = subform.textBox1.Text; 
    //...
}

But I have some troubles with editing a combobox (placed inside main form) by a sub-cs file. I can't simply create a new main window. I've edited Program.cs file:

static class Program
    {
        public static MainForm mainform; //main window reference
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(mainform = new MainForm());
        }
    }

Now I can access the combobox:

Program.mainform.comboCurves.Items.Add("test");

Is it any better solution for this?

1 Answers
Related