How to reference main form not instance of it

Hi experts

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

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSelectColour.SelectedIndex == 0)
            {
                cmbSelectColour.BackColor = Color.AliceBlue;
            }
            else if (cmbSelectColour.SelectedIndex == 1)
            {
                cmbSelectColour.BackColor = Color.Beige;
            }
            else if (cmbSelectColour.SelectedIndex == 2)
            {
                cmbSelectColour.BackColor = Color.BlueViolet;
            }
            else if (cmbSelectColour.SelectedIndex == 3)
            {
                cmbSelectColour.BackColor = Color.Crimson;
            }
            else if (cmbSelectColour.SelectedIndex == 4)
            {
                cmbSelectColour.BackColor = Color.DarkGreen;
            }
        }



private void btnExit_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            this.Close();
            switch (PassVariables.Instance.ClickButton)
            {
                case (1):
                    f1.button1.Select();
                    f1.button1.Text = "Hi Button 1";
                    PassVariables.Instance.BackColor = cmbSelectColour.BackColor;
                    f1.button2.BackColor = PassVariables.Instance.BackColor;
                    break;
                case (2):
                    f1.button2.Select();
                    f1.button2.Text = "Hi Button 2";
                    PassVariables.Instance.BackColor = cmbSelectColour.BackColor;
                    f1.button2.BackColor = PassVariables.Instance.BackColor;
                    break;
                case (3):
                    f1.button3.Select();
                    f1.button3.Text = "Hi Button 3";
                    PassVariables.Instance.BackColor = cmbSelectColour.BackColor;
                    f1.button3.BackColor = PassVariables.Instance.BackColor;
                    break;
                case (4):
                    f1.button4.Select();
                    f1.button4.Text = "Hi Button 4";
                    PassVariables.Instance.BackColor = cmbSelectColour.BackColor;
                    f1.button4.BackColor = PassVariables.Instance.BackColor;
                    break;
            }

            f1.ShowDialog();
            this.Show();
        }
}

Open in new window


Using c# i have a main form and a settings form, in the settings form i want to change the controls text, backcolor etc on the main form, if I use:    Form1 f1 = new Form1();  then this will only create a new instance of my main form and change the controls properties on that instead of changing them on my original main form (Form1), my question is how can I reference my main form (Form1) from my properties form (Form2) without having to create a new instance of it.
dizzycatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
((TextBox)Form1.controls.Find("MyControlID",false)).AProperty = SomeValue;

This was found from:
http://stackoverflow.com/questions/9517599/accessing-form-elements-dynamically
0
AndyAinscowFreelance programmer / ConsultantCommented:
public partial class Form2 : Form
    {
        private Form2()  //stop this being used
        {
            InitializeComponent();
        }

private Form1 _mainForm;  //use this when you want to reference the main form
public Form2(Form1 mainForm)
{
_mainForm = mainForm;   //set the value
            InitializeComponent();
}


and when you launch the settings form you need to change
new Form2()
to
new Form2(this)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dizzycatAuthor Commented:
Im trying this but getting an error with "controls" in Form1.controls.Find

((Button)Form1.controls.Find("button1", false)).BackColor = cmbSelectColour.BackColor;
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

AndyAinscowFreelance programmer / ConsultantCommented:
Use my suggestion, passing a reference to the first form into the second form is the 'standard' way to accomplish what you want.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try something more like:
    public partial class Form1 : Form
    {

        private void btns_Click(object sender, EventArgs e) // multiple buttons fire this same event?
        {
            Form2 f2 = new Form2();
            if (f2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Button btn = (Button)sender;
                btn.BackColor = PassVariables.Instance.BackColor;
            }
        }

    }

Open in new window


With this in Form2:
    public partial class Form2 : Form
    {

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSelectColour.SelectedIndex == 0)
            {
                cmbSelectColour.BackColor = Color.AliceBlue;
            }
            else if (cmbSelectColour.SelectedIndex == 1)
            {
                cmbSelectColour.BackColor = Color.Beige;
            }
            else if (cmbSelectColour.SelectedIndex == 2)
            {
                cmbSelectColour.BackColor = Color.BlueViolet;
            }
            else if (cmbSelectColour.SelectedIndex == 3)
            {
                cmbSelectColour.BackColor = Color.Crimson;
            }
            else if (cmbSelectColour.SelectedIndex == 4)
            {
                cmbSelectColour.BackColor = Color.DarkGreen;
            }

            // store the selected color so we can get it back in Form1:
            PassVariables.Instance.BackColor = cmbSelectColour.BackColor;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

    }

Open in new window


*Though, instead of using a static member in a class via "PassVariables.Instance.BackColor", you should instead just store the selected color in Form2 itself as a property, then use "f2" back in Form1 to access it after the dialog is dismissed.
0
dizzycatAuthor Commented:
Thanks Andy that worked perfectly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.