簡體   English   中英

如何在Windows應用程序中將數據從一種形式傳遞到另一種形式的文本框?

[英]How to pass data from one form to another form textbox in windows application?

我正在嘗試將變量從一種形式傳遞到另一種形式的文本框。 “變量”是基於用戶輸入的計算結果。

下面是父窗體(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中不會顯示。

到目前為止,我試圖將數據推回父表單,然后嘗試在失敗的文本框中顯示數據(我不知道哪里出了錯,建議我是否可以解決以下問題)

現在,我需要一種替代方法,例如:代替將數據推回父表單,我需要使變量可用於所有嘗試使用subform(formulainputs)的表單。

我該如何實現這個過程? 任何建議,不勝感激。

問題似乎是insertForm.Show()不會阻止按鈕處理程序的執行。 Show以非模式打開insertform

因此,在打開insertform之后,將繼續在button_generateformula_Click執行,並且當您退出using塊時, insertform被放置並因此被關閉。

為了解決這個問題,您可以改為調用insertForm.ShowDialog()


對於表單之間進行通信的不同方式,請在此處查看或直接在SO搜索框中鍵入communicate between forms

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM