2015-07-03 106 views
0

我在将数据从一种表单传递到另一种表单时遇到问题。我已经看过几个例子并尝试过,但没有任何工作。Winforms在表单之间传递值问题

在“估计”的形式,这是什么打开“EstimateTakeoffItemAdd”表...

private void TakeoffGridAdd(object sender, FormClosingEventArgs e) 
    { 
     MessageBox.Show(AutoID.ToString()); 

     int index = 0; 

     if (AutoID > 0) 
     { 
     } 
    } 

    private void addDialog() 
    { 
     if (treeView1.SelectedNode.Name.ToString() != "c") 
     { 
      using (var et = new EstimateTakeoffItemAdd(estID, Convert.ToInt32(treeView1.SelectedNode.Name.ToString()), Convert.ToInt32(SheetSelector.SelectedValue), Convert.ToInt32(BidItemSelector.SelectedValue), Convert.ToInt32(SortCodeSelector.SelectedValue))) 
      { 
       et.FormClosing += new FormClosingEventHandler(TakeoffGridAdd); 
       et.ShowDialog(); 
       AutoID = et.AddedID; 
      } 
     } 
    } 

在“EstimateTakeoffItemAdd”的形式,这是可以节省/关闭窗体的功能...

public int AddedID { get; set; } 
    public bool ItemAdded { get; set; } 

    private void radButton2_Click(object sender, EventArgs e) 
    { 
     pricemod = (Convert.ToDecimal(LaborText.Text) == dbLabor) ? 0 : 1; 
     pricemod = (Convert.ToDecimal(MaterialText.Text) == dbPrice) ? 0 : 1; 

     try 
     { 
      DBConnector db = new DBConnector(); 
      db.query = "SELECT Auto_increment FROM information_schema.tables WHERE table_name='tb_estimate_takeoff'";     
      this.AddedID = Convert.ToInt32(db.executeScalar()); 
     } 
     finally 
     { 
      try 
      { 
       DBConnector db = new DBConnector(); 
       db.query = "INSERT INTO tb_estimate_takeoff (item, estimate, sheet, qty, hrs, mat, biditem, phase, matunit_mod, labunit_mod, guid) VALUES (" + 
          "'" + DescriptionText.Text + "', " + 
          estID + ", " + 
          sheet + ", " + 
          QtyText.Value + ", " + 
          LaborText.Text + ", " + 
          MaterialText.Text + ", " + 
          biditem + ", " + 
          phase + ", " + 
          pricemod + ", " + 
          labormod + ", " + 
          "'" + guid + "'" + 
          ")"; 
       db.executeNonQuery(); 
      } 
      finally 
      { 
       ItemAdded = true; 
       this.Close(); 
      } 
     } 
    } 

我需要的是将AddedID恢复回主“估计”窗体。在关闭“EstimateTakeoffItemAdd”之前使用MessageBox我可以看到AddedID正在处理该表单,但它在“Estimate”表单上显示为0。

第二次打开对话框并关闭后,第一个AddedID随后可用于“估计”窗体。

我错过了什么?

+0

看起来很好。 '但是它在“估计”表单上显示0。“< - 你能告诉我们你在哪里做这个?另外,当你在调试器中遍历你的代码,并且在'AutoID = et.AddedID'行查看'et.AddedID'的值时,你看到了什么? – sstan

+0

通过使用FormClosing函数中的消息框...已在上面编辑。 – Mike

+0

你可以编辑你的文章,而不是把它放在评论中?这样,它会有适当的格式,其他人也会看到它。谢谢! – sstan

回答

1

这听起来像值得通过很好。你只是在错误的时间在错误的地方测试价值。

看起来您已经注册了TakeoffGridAdd事件处理程序,以在EstimateTakeoffItemAdd表单关闭时执行。

如果这是真的,那么该行:

et.ShowDialog(); 

...还没有恢复,因为形式还没有结束。这意味着分配AutoID = et.AddedID;也尚未执行。

因为你的方法EstimateTakeoffItemAdd之前分配是由运行,你总是出现被读取旧值。

0

谢谢@Hans Passant和@sstan。摆脱FormClosing事件处理程序解决了我的问题。

private void addDialog() 
    { 
     if (treeView1.SelectedNode.Name.ToString() != "c") 
     { 
      using (var et = new EstimateTakeoffItemAdd(estID, Convert.ToInt32(treeView1.SelectedNode.Name.ToString()), Convert.ToInt32(SheetSelector.SelectedValue), Convert.ToInt32(BidItemSelector.SelectedValue), Convert.ToInt32(SortCodeSelector.SelectedValue))) 
      { 
       if (et.ShowDialog() == DialogResult.OK) 
       { 
        AutoID = et.AddedID; 
        TakeoffGridAdd(); 
       } 
      } 
     } 
    }