1
votes

I have Form1 and Form2 simultaneously active, Form2 has a TabControl and I want button click event on Form1 to change 'Enable' property of tabControl on Form2

TabControl on Form2 is set to tabControl1.enabled = false; and Form1 acts as a login form for Form2, so I need a Login button on Form1 to enable 'tabControl1' on Form2

I can access the property by setting private System.Windows.Forms.TabControl tabControl1; to 'Public', still, using the following on button click event on Form1 doesn't do anything.

Form2 formnew2 = new Form2();
formnew2.tabControl1.Enabled = true;

Can someone please provide a simple example to help me understand or link to a previously answered question

2
To set the control to be public, don't change designer generated code, In your Form2 designer, select the tabControl1 and using property window change the Modifier property of your tabControl1 to be public. Also make sure you are showing the instance that you made it's tab control disabled. Also if you set tabControl1.enabled = false; in form load event of form 2, the tabCorntrol will be disabled.Reza Aghaei
@RezaAghaei Yes, that's exactly what I want, TabControl on Form2 to be disabled by default. Note that I can enable TabControl with a button click event on Form2 (the button obviously resides outside TabControl on Form2), but my objective is to enable it from Form1. BTW, Thanks for the Tip regarding Modifier.Arasa Ror
@RezaAghaei I set the Enable property of tabControl1 to false from property windowArasa Ror
So I suspect that you are using the wrong reference form 2. you probably have an open form 2, but you mistakenly create an instance of form 2 again here. You should pass an instance of form 2 to form 1 and use it, also you can find your instance of form 2 using Application.OpenFormsReza Aghaei
@RezaAghaei Thank you, That is exactly what it was, I was referencing a new instance instead of passing an instance from form2. Can you please post your comment as an answer so I can accept itArasa Ror

2 Answers

0
votes

In Form1 have a reference to Form2. In Form2 wrap the TabControl's Enable property in a public method and call the it from Form1

In Form1:

...
        Form2 form2;
        public Form1()
        {
            // initialize and show form2
            this.form2 = new Form2();
            this.form2.Show();
        }
...

in Form2:

...
        public void EnableTabControl()
        {
            this.tabControl1.Enabled = true;
        }
...

then in Form1 when the button is clicked:

private void btnLogin_Click(object sender, EventArgs e)
{
    // verify that it was initialized
    if (form2 != null)
    {
        this.form2.EnableTabControl();
    }
}
0
votes

It seems you are using the wrong reference of Form2. You probably have an open Form2, but you mistakenly create a new instance of Form2 again here in your Form1. So changing the new instance properties has no effect on previously opened instance.

You should pass an instance of Form2 to Form1 and use it.

Also you can find running instance of Form2 using Application.OpenForms