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?
You need to put
loginData
into a local variable inside thefrmVoiceOver
class to be able to access it from other methods. Currently it is scoped to the constructor: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 :-)
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
andForm2
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 ofForm2
, textbox ofForm1
gets updated.Following is the code of Form1
Following is the code of Form2
In order to pass the values we should store your data in a class which is derived from EventArgs
Now whenever text changes in Form2, the same text gets updated in textbox of Form1.