这个.close()也关闭了主窗体

本文关键字:窗体 close 这个 | 更新日期: 2023-09-27 18:22:19

已经想了大约一个小时了!

我做了一个应用程序,它有1个主窗体,在Program.cs中启动,就像这个

frmFleetMain frmW = new frmFleetMain();
frmW.iNIPathAndFile = GlobalInfo.iNIPathAndFile;
frmW.SetUser(GlobalInfo.Username);
Application.Run(frmW);

和1个子表单,以主表单的形式启动,如

                if (recordid != "")
                {
                    frmFleetSave frmsave = new frmFleetSave();
                    frmsave.Rowid = int.Parse(recordid);
                    frmsave.ShowDialog();                        
                   dgvMain.Rows[rowindx].Cells[0].Style.BackColor = Color.Red;                       
               oFuel.Filter = FLEET_FUEL.ColumnNames.FUEL_SEQUENCE + " = "+recordid;
                    oFuel.FLEET_NO = int.Parse(GlobalInfo.FLEETNO);
                    oFuel.Filter = "";
                    GlobalInfo.FLEETNO = "";
                }

在frmsave的btnclose事件中,我有这个代码

if (GlobalInfo.FLEETNO == "")
{
   DialogResult result = MessageBox.Show("Record will not be updated!Click OK to try again or CANCEL to close.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
   if (result == DialogResult.Cancel)
   {                    
      this.Close();//this closes both forms -this is not what i want
   }
}
else
{
   this.Close();//this closes frmsave only-this is what i want:)
}

我不明白为什么会发生这种事!帮助:)

已解决:

 if (recordid != "")
                {
                    frmFleetSave frmsave = new frmFleetSave();
                    frmsave.Rowid = int.Parse(recordid);
                    frmsave.ShowDialog();
                    if (frmsave.ssave)//get a bool variable indicating we can save! :-P
                    {
                        oFuel.Filter = FLEET_FUEL.ColumnNames.FUEL_SEQUENCE + " = " + recordid;
                        oFuel.FLEET_NO = int.Parse(GlobalInfo.FLEETNO);
                        oFuel.Filter = "";
                        GlobalInfo.FLEETNO = "";
                    }

}

这个.close()也关闭了主窗体

尝试将frmsave.ShowDialog();更改为frmsave.ShowDialog(this);

并将其他代码更改为:

        DialogResult result = MessageBox.Show(this, "Record will not be updated!Click OK to try again or CANCEL to close.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
        if (result == DialogResult.Cancel)
        {
            Close();
        }

这在我的电脑上运行良好…

已解决:

 if (recordid != "")
                {
                    frmFleetSave frmsave = new frmFleetSave();
                    frmsave.Rowid = int.Parse(recordid);
                    frmsave.ShowDialog();
                    if (frmsave.ssave)//get a bool variable indicating we can save! :-P
                    {
                        oFuel.Filter = FLEET_FUEL.ColumnNames.FUEL_SEQUENCE + " = " + recordid;
                        oFuel.FLEET_NO = int.Parse(GlobalInfo.FLEETNO);
                        oFuel.Filter = "";
                        GlobalInfo.FLEETNO = "";
                    }

}