1

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.

user3293926
  • 11
  • 1
  • 2

4 Answers4

3

Your answer is around here somewhere:

1) How to pass values between forms in c# windows application?

2) Passing data between forms

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?

7) Passing Data Between Forms

8) Get data from one textbox on form1 from another textbox on form2

Community
  • 1
  • 1
Selman Genç
  • 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
2

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.

Mukesh Rawat
  • 2,047
  • 16
  • 30
1

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.

Praveen
  • 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
0

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();
    }
}
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284