user5681611
user5681611

Reputation:

Passing data between forms (and back)

On Form1 I have a label with my total points. In Form2 I made a store were you can purchase things with those points. However I am able to pass the points value to Form2 but I can't figure out how I can send the points value back to Form1.

The code I use to send the value from Form1 to Form2.

Inventory inventory = new Inventory();
inventory.points = points.

I used the search function but since I just started writing code I find most of the given answers too confusing.

Upvotes: 0

Views: 59

Answers (2)

Dot_NET Pro
Dot_NET Pro

Reputation: 2123

Code a constructor for form2 class as below:

public Form2(string strTextBox)
{
InitializeComponent(); 
label1.Text=strTextBox;
}

On Form1 where you want to call Form2 and pass a value

Form2 frm=new Form2(textBox1.Text);
frm.Show();

Add a property in Form1 to retrieve value from textbox:

public string _textBox1
{
  get{return textBox1.Text;}
}

On Form2:

public string _textBox
{
set{label1.Text=value;}
}

Upvotes: 1

Michael Kanios
Michael Kanios

Reputation: 59

Pass the Form1 instance to Form2 when setting the points. This requires a Form1 public variable in Form2. e.g

Inventory inventory = new Inventory();
inventory.points = points;
inventory.form1 = this;

and when sending back in Form2

this.form1.points = this.points;

I hope this helps!

Upvotes: 0

Related Questions