如何在Windows应用程序中将数据从一个表单传递到另一个表单文本框?

时间:2016-05-16 17:21:30

标签: c# class interface controls windows-applications

我试图将变量从一个表单传递到另一个表单文本框。 '变量'是基于用户输入的计算结果。

下面是父表单(RuleInsertForm)的代码,我在其中调用子表单(Helpformula)来获取用户输入。

 public partial class RuleInsertForm : Form
    { 
    public string helpformulainputs;
    }

 private void RuleInsertForm_Load(object sender,EventArgs e)
    {

  if (helpformulainputs=="")
        {
            textBox_Inputs.Text = "";
        }

        else
        {
        textBox_Inputs.Text = helpformulainputs;
        }

      }

下面是子表单(Helpformula)的代码,其中我将结果变量(formulainputs)传递给父表单(RuleInsertForm)。

 public partial class HelpFormula : Form
{
    public string formulainputs = string.Empty;

   private void button_generateformula_Click(objectsender, EventArgs e)
    {
        using (RuleInsertForm insertform = new RuleInsertForm())
        {

                insertform.helpformulainputs = formulainputs;
                this.Close();
                insertform.Show();

        }
     }

  }

问题: 这些值将传递到文本框,但在UI中却没有显示出来。

到目前为止,我试图将数据推回到父表单,然后尝试在我失败的文本框中显示数据。(我不知道它出错的地方建议我,如果我可以解决下面的问题)

现在我需要一种替代方法,例如:不要将数据推回到父表单,而是需要使变量可用于尝试使用子表单的所有表单(formulainputs)

我怎样才能实现这个过程?任何建议都非常感谢。

1 个答案:

答案 0 :(得分:1)

问题似乎是insertForm.Show()不会阻止按钮处理程序的执行。 Showinsertform打开为非模态。

因此,在insertform打开后,将在button_generateformula_Click中继续执行,当您退出using块时,insertform将被处置并因此关闭。

要解决此问题,您可以致电insertForm.ShowDialog()

对于表单之间的不同通信方式,请查看here或只需在SO搜索框中输入communicate between forms

相关问题