I was just wondering exactly how you transfer string and integer variables between forms. Example on what I mean would be having the user input their name and age which would transfer and show in another form and have this be activated by a button.
-
2There are hundreds of questions asking this. I am voting to close this because it is a duplicate. – Simon Whitehead Feb 11 '14 at 04:23
4 Answers
Your answer is around here somewhere:
1) How to pass values between forms in c# windows application?
3) switching between forms without loss of information
4) windows.form c# moving between forms
5) Communicate between two windows forms in C#
6) How to share data between forms?
8) Get data from one textbox on form1 from another textbox on form2

- 1
- 1

- 100,147
- 13
- 119
- 184
-
I don't know who down voted this, but I think this is a good answer that shows it is a duplicate question. One of these is going to give the poster a cut and paste answer for their particular code. – SeeMoreGain Feb 11 '14 at 04:29
You can achieve this by using Simple Events and delegate concept. Publish event from your Form 1
and you can subscribe that in your Form 2
page. Delegate
will be act as a communication channel between Event
and Event handler
.

- 2,047
- 16
- 30
Use a Global static class for saving values. And just use the object of that class at the another location.And create new object of the class whenever the task is completed.

- 267
- 1
- 9
-
This is generally considered bad practice. Software should always be designed around the idea of least privilege. So giving global access to a variable when only one form needs it is not a good idea. See one of the other linked answers that uses delegate methods or any number of other ways of directly communicating between forms. – SeeMoreGain Feb 11 '14 at 04:28
You could pass the values from the main form to the child form using public properties.
Something like
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
ChildForm c = new ChildForm();
c.StringValue = "TADA";
c.IntValue = 42;
c.Show();
}
}
public partial class ChildForm : Form
{
public string StringValue{get; set;}
public int IntValue { get; set; }
public ChildForm()
{
InitializeComponent();
}
}

- 162,879
- 31
- 289
- 284