表单调用另一个表单,只是为了确认用户想要继续

时间:2013-04-30 20:07:41

标签: c# winforms mdi

我在VS2008环境中使用C#。

我有一个名为frmStrategy5YrPlan的表单,其中包含一个数据网格视图,用于填充数据,然后允许用户键入新的预测数据。完成后,用户将单击一个按钮上传他们输入的新数据,我希望显示另一个表单,要求他们确认他们要这样做(因为该过程会删除所有旧数据并替换为'我进入了。)

我总是被表单欺骗,但基本上我希望我的初始表单加载第二个表单,评估用户是否选择确认加载数据(或不加载),然后使用初始表单中的代码处理数据。

这是我的初始形式,今天工作正常而没有调用第二种形式(我正在询问)。下面是初学者的第二种形式的代码和我添加的按钮的工作方式,只是试图让两种形式相互交流。在另一部分中调用表单时,总是让我感到困惑。我对MDIParents不太熟悉,但我在这里尝试过,我的初始表单没有设置为一个,所以我不确定这是否是解决方案。

public void btnUploadNewStrategy5YrPlan_Click(object sender, EventArgs e)
        {
            //this.ConfirmForm.Text = "DSC_0 Staged";
            //this.ConfirmForm.EnableReport(false);
            //this.ConfirmForm.Type = frmConfirmForecastUpload.LoadType.DSC_0;
            //this.ConfirmForm.MdiParent = this;
            this.ConfirmForm.Top = this.Height / 3 - this.ConfirmForm.Height / 2;
            this.ConfirmForm.Left = this.Width / 2 - this.ConfirmForm.Width / 2;
            this.ConfirmForm.Show();
            Cursor.Current = Cursors.WaitCursor;

            SqlCommand cmd = null;
            SqlDataReader dr = null;
            StringBuilder sql = new StringBuilder();

            try
            {
                var dtForecast = new DataTable();
                dtForecast.Columns.Add("Unit");
                dtForecast.Columns.Add("Year");
                dtForecast.Columns.Add("Period");
                dtForecast.Columns.Add("Acct");
                dtForecast.Columns.Add("Descr");
                dtForecast.Columns.Add("DEFERRAL_TYPE");
                dtForecast.Columns.Add("NDC_Indicator");
                dtForecast.Columns.Add("Mgmt Cd");
                dtForecast.Columns.Add("Prod");
                dtForecast.Columns.Add("Node");
                dtForecast.Columns.Add("Curve_Family");
                dtForecast.Columns.Add("Sum Amount");
                dtForecast.Columns.Add("Base Curr");
                dtForecast.Columns.Add("Ledger");

                for (int ii = 0; ii < grd1.Rows.Count; ii++)
                {
                    int myCountryKey = int.Parse(grd1.Rows[ii].Cells["colCountryKey"].FormattedValue.ToString());
                    int myCurrencyKey = int.Parse(grd1.Rows[ii].Cells["colCurrencyKey"].FormattedValue.ToString());
                    string myCurrencyCode = grd1.Rows[ii].Cells["colCurr"].FormattedValue.ToString();
                    int myDeferralTypeKey = int.Parse(grd1.Rows[ii].Cells["colDeferralTypeKey"].FormattedValue.ToString());
                    string myDeferralTypeCode = grd1.Rows[ii].Cells["colDeferralType"].FormattedValue.ToString();
                    long myAccount = 6018110000;
                    string myAcctDesc = "";
                    string myCurveFamily = "AH_DM"; //hard coded to AH_DM for now since only DM is in AMORT
                    myDataRegionKey = int.Parse(grd1.Rows[ii].Cells["colRegionKey"].FormattedValue.ToString());
                    myNodeKey = int.Parse(grd1.Rows[ii].Cells["colNodeKey"].FormattedValue.ToString());
                    string myNodeCode = grd1.Rows[ii].Cells["colNodeCode"].FormattedValue.ToString();
                    mySubNodeKeyString = grd1.Rows[ii].Cells["colSubNodeKey"].FormattedValue.ToString();

                    int mySubNodeKey = int.TryParse(mySubNodeKeyString, out mySubNodeKey) ? mySubNodeKey : 0;


                    //Run query to get BU/MCC defaults for this countryKey
                    cmd = util.SqlConn.CreateCommand();
                    cmd.CommandTimeout = 600;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "proc_get_country_defaults";
                    cmd.Parameters.Add("@node_key", SqlDbType.VarChar).Value = myNodeKey;
                    cmd.Parameters.Add("@country_key", SqlDbType.VarChar).Value = myCountryKey;
                    cmd.Parameters.Add("@division_key", SqlDbType.VarChar).Value = myDivisionKey;
                    dr = cmd.ExecuteReader();

                    dr.Read();
                    int myMCC = (int.Parse(dr["management_code"].ToString()));
                    int myBusinessUnit = (int.Parse(dr["business_unit_code"].ToString()));
                    dr.Close();

                    //Run query to get product defaults for this NodeKey
                    cmd = util.SqlConn.CreateCommand();
                    cmd.CommandTimeout = 600;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "proc_get_node_defaults";
                    cmd.Parameters.Add("@node_key", SqlDbType.VarChar).Value = myNodeKey;
                    dr = cmd.ExecuteReader();

                    dr.Read();
                    int myProductKey = (int.Parse(dr["product_key"].ToString()));                    
                    dr.Close();

                    //Run query to get product defaults for this SubNodeKey
                    cmd = util.SqlConn.CreateCommand();
                    cmd.CommandTimeout = 600;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "proc_get_sub_node_defaults";
                    cmd.Parameters.Add("@sub_node_key", SqlDbType.VarChar).Value = mySubNodeKey;
                    dr = cmd.ExecuteReader();

                    //Only overwrite the product key with subnode default if
                    //subnodekey is not null and region is Latin America
                    if (mySubNodeKey > 0 && myDataRegionKey == 100001)
                    {
                        dr.Read();
                        myProductKey = (int.Parse(dr["product_key"].ToString()));
                        dr.Close();
                    }
                    else
                    {
                        dr.Close();
                    }          

                    //Loop through the 5 years
                    for (int jj = 1; jj < 6; jj++)
                    {
                        if (myDataYear > myMostRecentActualYear)
                        {

                            string myYearCount = jj.ToString().PadLeft(2, '0');
                            string myColName = "col" + myYearCount;
                            double mySumAmt = Convert.ToDouble(grd1.Rows[ii].Cells[myColName].Value) * 1000 / 12;  //Convert annual amount to monthly

                            if (mySumAmt != 0)
                            {

                                //Loop through 12 months
                                for (int nn = 1; nn < 13; nn++)
                                {
                                    int myDataYearToWrite = myDataYear - 1 + jj;

                                    DataRow _fcst = dtForecast.NewRow();
                                    _fcst["Unit"] = myBusinessUnit;
                                    _fcst["Year"] = myDataYearToWrite;
                                    _fcst["Period"] = nn;
                                    _fcst["Acct"] = myAccount;
                                    _fcst["Descr"] = myAcctDesc;
                                    _fcst["DEFERRAL_TYPE"] = myDeferralTypeCode;
                                    _fcst["NDC_Indicator"] = "";
                                    _fcst["Mgmt Cd"] = myMCC;
                                    _fcst["Prod"] = myProductKey;
                                    _fcst["Node"] = myNodeCode;
                                    _fcst["Curve_Family"] = myCurveFamily;
                                    _fcst["Sum Amount"] = mySumAmt;
                                    _fcst["Base Curr"] = myCurrencyCode;
                                    _fcst["Ledger"] = "CORE";

                                    dtForecast.Rows.Add(_fcst);
                                }
                            }
                        }
                    }                                     

                }

                //Export data table to Excel file
                Excel.Application xlApp;
                Excel.Workbook xlWorkBook;
                Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;

                xlApp = new Excel.ApplicationClass();

                xlWorkBook = xlApp.Workbooks.Add(misValue);

                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                int x = 0;
                int y = 0;

                //// storing header part in Excel 
                for (int q = 1; q < dtForecast.Columns.Count + 1; q++)
                {
                    xlWorkSheet.Cells[1, q] = dtForecast.Columns[q - 1].ToString().ToUpper() + "\t";
                    string myHeader = dtForecast.Columns[q - 1].ToString().ToUpper();
                }

                // storing grid data part in Excel 
                for (x = 0; x <= dtForecast.Rows.Count - 1; x++)
                {
                    for (y = 0; y <= dtForecast.Columns.Count - 1; y++)
                    {
                        string myData = Convert.ToString(dtForecast.Rows[x][y]);

                        xlWorkSheet.Cells[x + 2, y + 1] = myData;
                    }
                }

                string tempPath = Path.GetTempPath();  //System.IO.Path.GetTempPath();
                string fileName = Guid.NewGuid().ToString(); //+ extension;
                myFileSave = tempPath + fileName;

                xlWorkBook.SaveAs(myFileSave, Excel.XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);

                xlApp.Quit();

                //Kill Excel objects
                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);

                //MessageBox.Show("Excel file created , you can find the file c:\\AMORT_data.xls");

                //This will import forecast to the zstbl_sol_cost staging table, and validate the data
                ImportStrategy5YrPlan();
                //This will load the data into tbl_cohort and then call Amortizer
                LoadStrategy5YrPlan();

            }

            catch (Exception ex)
            {
                util.LogError(ex);
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                if (dr != null) dr.Dispose();
                if (cmd != null) cmd.Dispose();
            }


            //dtForecast.Load.Add(theRow);
        }

新表格

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AmortClient
{
    public partial class frmConfirmForecastUpload : Form
    {
        public frmConfirmForecastUpload()
        {
            InitializeComponent();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            frmStrategy5YrPlan console = (frmStrategy5YrPlan)this.MdiParent;
            Cursor.Current = Cursors.WaitCursor;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }

2 个答案:

答案 0 :(得分:6)

只需使用消息框并在其上显示提示按钮并注意成功。

MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);

答案 1 :(得分:0)

上面的答案是一个很好的方法,但如果由于某种原因你可能会显示一些额外的信息,并且需要的不仅仅是一个消息框,你可以使用Form.ShowDialog http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog(v=VS.71).aspx

我正在工作,所以没有VS向你展示一个有效的例子,但是如果你需要的话,只需要搜索一个教程。

当您需要打开第二个表单时,从第一个表单

if(form2.ShowDialog = DialogResult.OK) {

}