0

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.

Gowtham Ramamoorthy
  • 896
  • 4
  • 15
  • 36

1 Answers1

1

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.

Community
  • 1
  • 1
René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • awesome man... thanks a lot... it got worked.. you saved my time :) I will mark this as an answer :)_ – Gowtham Ramamoorthy May 16 '16 at 17:32
  • just a qucik question... this one resolved my issue.. but now im having two insertform in the UI display .... I can use an alternate method to handle this but is ther any suggestion from your end ? – Gowtham Ramamoorthy May 16 '16 at 17:37
  • please refer to the link or search term I added to my answer. there are a lot of threads about that on stackoverflow already. – René Vogt May 16 '16 at 17:39