2013-04-30 22 views
0

我在VS2008環境中使用C#。表單調用另一個表單,只是爲了確認用戶想繼續

我有一個名爲frmStrategy5YrPlan的表單,它有一個數據網格視圖,其中填充數據,然後允許用戶鍵入新的預測數據。完成後,用戶將點擊一個按鈕上傳他們輸入的新數據,我希望顯示另一個表單,要求他們確認他們想要這樣做(因爲該過程會刪除所有舊數據並替換「已經進入)。

我總是被表格欺騙,但本質上我想我的初始表單加載第二個表單,評估用戶是否選擇了確認加載數據(或不是),然後使用代碼在初始表單中處理數據。

這裏是我的初始表格,今天沒有調用第二個表單(我在問)可以正常工作。下面是第二種形式的代碼和我添加的按鈕的初級工作,試圖讓2個表單相互交流。在另一部分中調用表單時,它總是讓我感到困惑。我;我不太熟悉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) 
     { 

     } 
    } 

回答

6

只需使用一個消息框,並有它提示按鈕,看成功。

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

壯觀。我真的讓它變得比需要更困難(這句話很可能寫在我的墳墓裏)。 – 2013-04-30 21:48:32

0

上面的答案是做一個偉大的方式,但如果出於某種原因你可能顯示一些產生額外的信息和需要的不僅僅是一個消息框,越用Form.ShowDialog http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog(v=VS.71).aspx

我在工作中,所以沒有VS向你展示一個工作示例atm,但只是在需要時尋找教程,但基本上。

從第一形態時需要打開第二形式你

如果(form2.ShowDialog = DialogResult.OK) {

}

相關問題