应用程序关闭时,C#代码(方法)运行

时间:2013-04-05 11:35:51

标签: c# winforms methods close-application

我有一个按钮,当按下它时会关闭当前表格并打开同一个类的新表单 - 即它会以原始状态打开一个新表单。

我有另一个具有相同功能的按钮,但是我尝试在代码中调用一个函数,所以当新表单打开时,它会运行importGantt()表单的一个函数。

我遇到的问题是,当我单击按钮时,它会关闭当前表单并按预期打开一个新表单,但在关闭应用程序之前它不会调用importGantt()函数。

有什么想法吗?

非常感谢。

private void browseFileToolStripMenuItem_Click(object sender, EventArgs e)
    {
        clearAndImport();
    }

private void clearAndImport()
    {
        this.Hide();
        Dashboard dashboard = new Dashboard();
        dashboard.ShowDialog();
        dashboard.importGantt();
        this.Close();
    }

private void importGantt()
    {
        // Edit Interface
        btnImport.Visible = false;
        dataCapPlan.Visible = true;
        dataMilestones.Visible = true;
        pnlGantt.Visible = true;
        Graphics ganttGraphics = pnlGantt.CreateGraphics();

        // Draw axis


        // Import Files
        fileCapPlan.Title = "Select Capital Plan File";
        fileCapPlan.Filter = "Excel Workbook (.xlsx)|*.xlsx";
        DialogResult resCapPlan = fileCapPlan.ShowDialog();
        if (resCapPlan == DialogResult.OK)
        {
            cnStr = cnStr + fileCapPlan.FileName;
        }
        else
        {
            MessageBox.Show("Error: Unable to import file");
        }
        fileMilestones.Title = "Select Milestones File";
        fileMilestones.Filter = "Excel Workbook (.xlsx)|*.xlsx";
        DialogResult resMilestones = fileMilestones.ShowDialog();
        if (resMilestones == DialogResult.OK)
        {
            cnStr2 = cnStr2 + fileMilestones.FileName;
        }
        else
        {
            MessageBox.Show("Error: Unable to import file");
        }

        // Use OleDb connection to import Excel data
        using (OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + cnStr + ";Extended Properties=" + "'EXCEL 12.0 Xml;HDR=YES'"))
        {
            using (OleDbDataAdapter adapter = new OleDbDataAdapter(sqlSelectAll, cn))
            {
                adapter.Fill(dtCapPlan);
                dataCapPlan.DataSource = dtCapPlan;
                dataCapPlan.AutoResizeColumns();
            }
        }
        using (OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + cnStr2 + ";Extended Properties=" + "'EXCEL 12.0 Xml;HDR=YES'"))
        {
            using (OleDbDataAdapter adapter = new OleDbDataAdapter(sqlSelectAll, cn))
            {
                adapter.Fill(dtMilestones);
                dataMilestones.DataSource = dtMilestones;
                dataMilestones.AutoResizeColumns();
            }
        }

        // Draw Gantt Chart
        foreach (DataRow rowCapPlan in dtCapPlan.Rows)
        {
            id = rowCapPlan["Program ID"].ToString();

            foreach (DataRow rowMilestone in dtMilestones.Rows)
            {
                if (id == rowMilestone["Program ID"].ToString())
                {
                    // calculate space in days from todays date and the milestone date
                    msDate = Convert.ToDateTime(rowMilestone["Milestone Date"]);
                    msTimespan = msDate - calDate;
                    msIntDate = (int)msTimespan.TotalDays + 1;
                    tTimespan = tDate - calDate;
                    tIntDate = (int)tTimespan.TotalDays + 1;
                    ganttPlotSpace = msIntDate - tIntDate;

                    // Draw each milestone or gateway which is not yet complete
                    if (rowMilestone["% Complete"].ToString() != "100")
                    {
                        taskname = rowMilestone["Task Name"].ToString();
                        if (taskname == "Gateway 1" || taskname == "Gateway 2" || taskname == "Gateway 3" || taskname == "Gateway 4" || taskname == "Gateway 5")
                        {
                            Rectangle gw = new Rectangle(startx + ganttPlotSpace, starty - 4, 2, 11);
                            ganttGraphics.DrawRectangle(gwPen, gw);
                            ganttGraphics.FillRectangle(gwBrush, gw);
                        }
                        else
                        {
                            Rectangle ms = new Rectangle(startx + ganttPlotSpace + 1, starty, 2, 2);
                            ganttGraphics.DrawRectangle(msPen, ms);
                            ganttGraphics.FillRectangle(msBrush, ms);
                        }
                        ganttGraphics.DrawLine(linePen, startx - 10, starty - 11, pnlGantt.Right, starty - 11);
                    }
                }
            }
            starty = starty + 22;
        }
        ganttGraphics.DrawLine(linePen, startx - 10, starty + 11, pnlGantt.Right, starty + 11);
    }

甘特图像 enter image description here

clearAndImport方法后的

图像(由用户修复) enter image description here


根据Brij的指导:

好的,所以在这个指导几乎可行的情况下,代码现在如下......

现在打开新表单并运行import方法,但是,它似乎是在循环中运行它。即它成功运行显示甘特图,但然后再次尝试运行导入甘特图方法。

bool clear;



public Dashboard(bool clear = false)
    {
        InitializeComponent();
        dataCapPlan.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataCapPlan_ColumnHeaderMouseClick);

        this.clear = clear;
        this.Load += new EventHandler(Dashboard_Load);
    }
    private void Dashboard_Load(object sender, EventArgs e)
    {
        if (this.clear)
        {
            this.importGantt();
        }
    }

// Clear and import method
    private void clearAndImport()
    {
        this.Hide();
        Dashboard dashboard = new Dashboard();
        dashboard.clear = true;
        dashboard.ShowDialog();
        this.Close();
    }

2 个答案:

答案 0 :(得分:1)

我认为您所指的方法如下:

private void clearAndImport()
{
        this.Hide();
        Dashboard dashboard = new Dashboard();
        dashboard.ShowDialog();
        dashboard.importGantt();
        this.Close();
}

您正在呼叫dashboard.ShowDialog()。因此,在关闭“仪表板”表单之前,下一行代码(dashboard.importGantt())将不会被调用。我建议你在构造函数中调用importGantt()或在Dashboard表单中调用Load事件。您也可以在dashboard.importGantt(上方移动dashboard.ShowDialog())来更改代码序列。


根据您的评论,我建议,修改Dashboard类的构造函数以接受布尔参数并使其成为可选(默认为false)。如果传递了true,则只调用importGantt()。所以它会像:

public Dashboard(bool clear = false)
{
   InitializeComponent();
   if(clear)
   {
       this.importGantt();
   }
}

,你clearAndImport()方法就像:

private void clearAndImport()
{
        this.Hide();
        Dashboard dashboard = new Dashboard(true);
        dashboard.ShowDialog();
        this.Close();
}

根据我上次的评论,试试这个:

bool clear = false;
public Dashboard(bool clear = false)
{
        InitializeComponent();
        this.clear = clear;
        this.Load += new EventHandler(Dashboard_Load);
}

void Dashboard_Load(object sender, EventArgs)
{
    if(this.clear)
    {
       this.importGantt();
    }
}

答案 1 :(得分:0)

尝试使用dashboard.Show();代替dashboard.ShowDialog();

using (var dashboard = new Dashboard())
{
    dashboard.Show(); // Show the form
    dashboard.importGantt(); // Call
    dashboard.Close(); // Close it when you're done...
}