当前位置: 动力学知识库 > 问答 > 编程问答 >

winforms - How to change 'Enable' property of tabcontrol from Form1 on Form2, C#, Visual studio

问题描述:

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

网友答案:

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

网友答案:

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();
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: