你好我有一个主表单(Form1)和一个子表单(Form5)。如果我第一次单击按钮,我希望它打开一个新表单。然而,如果它第二次被点击我想要它打开我第一次打开的表格,拿着我将填写的表格数据(文本框等)。这就是我所拥有的: .cpp文件:
System::Void Form1::button5_Click(System::Object^ sender, System::EventArgs^ e){
formclick++;
if (formclick == 1)
{
Form5 ^dos1 = gcnew Form5(this, MyArray, MyArray1);
dos1->Show();
}
if (formclick==2)
{
otherform->Show();
}
Form1.h文件:
> Form1(System::Windows::Forms::Form ^ Form5)
> {
>
>
> otherform = Form5;
> InitializeComponent();
> } public: System::Windows::Forms::Form ^ otherform;
然而我收到错误: 类型' System.NullReferenceException'的未处理异常发生在System.Windows.Forms.dll
中附加信息:未将对象引用设置为对象的实例。
TIA
答案 0 :(得分:0)
您将新创建的表单存储在button5_click函数的本地范围内(在dos1
变量中),这意味着当函数存在时可以自由地从内存中删除。
您提供的代码摘录有点混乱,但您可以将点击功能更改为以下内容:
System::Void Form1::button5_Click(System::Object^ sender, System::EventArgs^ e){
if (!otherForm)
otherForm = gcnew Form5(this, MyArray, MyArray1);
else
otherform->Show();
}
代码未经过测试,但主要的是您需要将新创建的表单存储在类成员中,而不仅仅是本地点击功能!
此致 甚至