在UserControl错误面板强制转换中设置来自父窗体的字符串



我的问题是,当我试图声明我的父窗体时,它会出现一个错误,即:

System.InvalidCastException: "The object of type `System.Windows.Forms.Panel` cannot be converted to type "xxx.Form1".

我不知道如何解决这个问题,通常情况下效果很好。父窗体代码只是一个public string EinstellungenGeändert,而我的UserControl代码是:

Form1 myParent = (Form1)this.Parent;
myParent.EinstellungenGeändert = true;

有人知道原因或有可能的解决方案吗?

似乎是这样。代码中的"Parent"指的是一种类型的"System"。Windows。表格。与"Form1"类型不同的面板"。

你确定你的Form1类型真的是"这个"吗。家长?试着提高一个级别(this.Parent.PParent(,如果需要的话再提高一次,尽管我不知道你想达到什么。

最新更新