0

我需要一些帮助。我已经阅读这个网站好几天了,并且阅读了很多关于从另一个表单控制按钮属性的技巧。Youtube 上什至有一个视频,它对我来说是独立的,但是当我在我的应用程序中实现它时,它会抛出 NullReferenceException。

假设我在 Form1 上有一个工具条菜单。单击 Kalibracio 选项会打开第二个表单(也称为 Kalibracio - 不是 Form2)。然后,单击菜单中的 Proba 应该禁用 Kalibracio 表单上的一个普通按钮,该按钮的属性设置为公共。Form1上的代码如下:

    private void kalibracioToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Kalibracio Kalibr = new Kalibracio(this);
        Kalibr.Owner = this;
        Kalibr.Show();
    }

    private void probaToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (Application.OpenForms.OfType<Kalibracio>().Any())
            (this.Owner as Kalibracio).button1.Enabled = false;
    // the above line throws a NullReferenceExcteption if Kalibracio form is open (Kalibracio is null)
    }

我错过了什么?

4

1 回答 1

0

LoL just had to declare globaly an instance of Kalibracio, open it and then access it's properties from all other methods. I tried this approach at first, but my problem was that I was creating and instance locally, then I had to create another one in some other method because I couldn't address the former one created locally, and ofc it didn't work...

于 2013-08-28T17:01:27.793 回答