1

我将按钮可见属性设置为 false Form2Form2当我Form2Form1.

我试过这个:

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.Show();
    f2.button1.Visible = true;
    f2.button1.Location = new Point(200, 200);
}
4

3 回答 3

4

在 Form2 中创建一个方法

public void setButton1Visible(boolean flag){
      this.button1.Visible = flag;
}

您不能直接从 Form1 访问该按钮。(实际上你可以,但这不是解决它的正确方法。

private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
        f2.setButton1Visible(true);
    }
于 2013-07-07T12:11:44.330 回答
1

我认为 button1 被声明为私有的。如果您将 button1 声明为公开,则您的代码将起作用。

public System.Windows.Forms.Button button1;
于 2013-07-08T09:07:53.057 回答
1

想象一下您的控件在表单 1 中。从控件属性窗口中设置相应的控件“修饰符 = public”

表格 1

    private void ShowForm2_Click(object sender, EventArgs e)
    {
        Form2 NewForm = new Form2();
        NewForm.Owner = this;
        NewForm.Show();
    }

表格 2

    private void ChangeProperty_Click(object sender, EventArgs e)
    {
       (this.Owner as Form1).MyButton.Visible = false;
    }

    //while doing this Control In Form1 will be hidden :)
于 2016-08-27T13:25:53.357 回答