Passing Values Between Windows Forms c# [duplicate]

I am struggling to work out how to pass values between forms. I have four forms and I want to pass the information retrieved by the Login to the fourth and final form.

This is what I have so far.

In this function:

private void btnLogin_Click(object sender, EventArgs e)

I have deserialized the data I want like this:

NewDataSet resultingMessage = (NewDataSet)serializer.Deserialize(rdr);

Then, when I call the next form I have done this:

Form myFrm = new frmVoiceOver(resultingMessage);
myFrm.Show();

Then, my VoiceOver form looks like this:

public frmVoiceOver(NewDataSet loginData)
{
    InitializeComponent();
}

private void btnVoiceOverNo_Click(object sender, EventArgs e)
{
    this.Close();
    Form myFrm = new frmClipInformation();
    myFrm.Show();
}

When I debug, I can see the data is in loginData in the second form, but I cannot seem to access it in the btnVoiceOverNo_Click event. How do I access it so I can pass it to the next form?


Solution 1:

You need to put loginData into a local variable inside the frmVoiceOver class to be able to access it from other methods. Currently it is scoped to the constructor:

class frmVoiceOver : Form
{
    private NewDataSet _loginData;

    public frmVoiceOver(NewDataSet loginData)
    {
        _loginData = loginData;

        InitializeComponent();
    }

    private void btnVoiceOverNo_Click(object sender, EventArgs e)
    {
        // Use _loginData here.
        this.Close();
        Form myFrm = new frmClipInformation();
        myFrm.Show();
    }
}

Also, if the two forms are in the same process you likely don't need to serialize the data and can simply pass it as a standard reference to the form's constructor.

Google something like "C# variable scope" to understand more in this area as you will encounter the concept all the time. I appreciate you are self-taught so I'm just trying to bolster that :-)

Solution 2:

In various situations we may need to pass values from one form to another form when some event occurs. Here is a simple example of how you can implement this feature.

Consider you have two forms Form1 and Form2 in which Form2 is the child of Form1. Both of the forms have two textboxes in which whenever the text gets changed in the textbox of Form2, textbox of Form1 gets updated.

Following is the code of Form1

private void btnShowForm2_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();            
    form2.UpdateTextBox += new EventHandler<TextChangeEventArgs>(txtBox_TextChanged);
    form2.ShowDialog();        
}

private void txtBox_TextChanged(object sender, TextChangeEventArgs e)
{
    textBox1.Text = e.prpStrDataToPass;            
}

Following is the code of Form2

public event EventHandler<TextChangeEventArgs> UpdateTextBox;

private string strText; 

public string prpStrText
{
     get { return strText; }           
     set
     {
          if (strText != value)
          {
               strText = value;
               OnTextBoxTextChanged(new TextChangeEventArgs(strText));
          }
     }
}

private void textBox_Form2_TextChanged(object sender, EventArgs e)
{
     prpStrText = txtBox_Form2.Text;
}

protected virtual void OnTextBoxTextChanged(TextChangeEventArgs e)
{            
     EventHandler<TextChangeEventArgs> eventHandler = UpdateTextBox;
     if (eventHandler != null)
     {
           eventHandler(this, e);
     }
}

In order to pass the values we should store your data in a class which is derived from EventArgs

public class TextChangeEventArgs : EventArgs
{
    private string strDataToPass;

    public TextChangeEventArgs(string _text)
    {
        this.strDataToPass = _text;
    }

    public string prpStrDataToPass
    {
        get { return strDataToPass; }
    }
}

Now whenever text changes in Form2, the same text gets updated in textbox of Form1.