Gowtham Ramamoorthy
Gowtham Ramamoorthy

Reputation: 896

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

I'm trying to pass a variable from one form to another form textbox. The 'variable' is a result of a calculation based on the user inputs.

Below is the code for the parent form(RuleInsertForm) where I'm calling the subform(Helpformula) to get the user inputs.

 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;
        }

      }

Below is the code for the subform(Helpformula) where i'm passing the result variable(formulainputs) to the parent form(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();

        }
     }

  }

Problem: The values are getting passed to the text box but in the UI its not getting dispalyed.

so far I tried to push data back to parent form and then tried to display the data in the textbox where I failed.(I dont know where it went wrong suggest me if I can resolve the below one)

Now I need an alternative method to this for eg: instead of pushing the data back to parent form i need to make the variable available for all the forms trying to use the subform(formulainputs)

How can I acheive this process ? any suggestions are much appreciated.

Upvotes: 0

Views: 107

Answers (1)

René Vogt
René Vogt

Reputation: 43916

The problem seems to be that insertForm.Show() does not block the execution of your button handler. Show opens the insertform as non-modal.

So after insertform is opened, the execution is continued in button_generateformula_Click and when you exit the using block, the insertform is disposed and therefore closed.

To solve this you may call insertForm.ShowDialog() instead.


For different ways of communicating between Forms look here or simply type communicate between forms into the SO search box.

Upvotes: 1

Related Questions