0

押すと現在のフォームを閉じ、同じクラスの新しいフォームを開くボタンがあります。つまり、新しいフォームを元の状態で開きます。

同じ機能を持つ別のボタンがありますが、コードで関数を呼び出そうとするので、新しいフォームが開いたときにフォーム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);
    }

ガント付き画像 ここに画像の説明を入力

clearAndImport メソッド後の画像 (ユーザーにより修正済み) ここに画像の説明を入力


Brij のガイダンスに従って:

さて、これはほとんど機能するガイダンスで、コードは次のようになりました...

これで新しいフォームが開き、インポート メソッドが実行されますが、ループで実行されているようです。つまり、ガントを表示して正常に実行されますが、インポート ガント メソッドを再度実行しようとします。

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();
    }
4

2 に答える 2

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();
   }
}

youclearAndImport()メソッドは次のようになります。

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();
    }
}
于 2013-04-05T11:44:34.857 に答える
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...
} 
于 2013-04-05T11:43:08.030 に答える