0

I am making a trading game for school and I have found myself in quite the conundrum.

I have two forms, my form1 is my main form and it has my balance textbox in it and it also has a textbox where it says money due. From this form you can open another form (form2) to get a loan.

In my form2 (loan) you can type in the amount you want to get and when you type in that amount it puts in 25% interest on that amount.

What I want to do is when you click the submit button, the amount you put into the textbox goes into your balance, and then the amount with interest goes into the money owed textbox.

What I can't figure out is how to pass these values from form2 to form1 to add to whatever values are already in those textboxes.

This is what I have when you click to open the loan form

private void loansToolStripMenuItem_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.ShowDialog();
}

And this is what I have when you try and submit the values, it is unfinished but I am completely stuck on how to do this

private void button1_Click(object sender, EventArgs e)
{
    Form1 testform = new Form1();

    int tmp;

    Int32.TryParse(textBox2.Text, out tmp);
    testform.textBox39.Text = tmp.ToString();
    this.Close();
}

Can anyone suggest any simple suggestions to do this?

D McCracken
  • 33
  • 1
  • 8
  • 1
    Possible duplicate of [How to pass values between forms in c# windows application?](https://stackoverflow.com/questions/1205195/how-to-pass-values-between-forms-in-c-sharp-windows-application) – PaulF Jun 19 '17 at 09:15

3 Answers3

0

Give Form2 a property that holds the amount. You can access it after Form2 was closed (after ShowDialog):

public partial class Form2 : Form
{
    public int Amount { get; private set; } = 0; 

    private void button1_Click(object sender, EventArgs e)
    {
        int tmp;

        Int32.TryParse(textBox2.Text, out tmp);

        this.Close();
    }
}

Access the value like this:

private void loansToolStripMenuItem_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.ShowDialog();
    textBox39.Text = form2.Amount.ToString();
}
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
0

In Form 2 You need public PassValue proberty, and in button1_Click function you must set DialogResult = DialogResult.OK

public partial class Form2 : Form { public int PassValue { get; set; }

private void button1_Click(object sender, EventArgs e)
{
    int tmp;
    Int32.TryParse(textBox2.Text, out tmp);
    this.PassValue = tmp;
    this.DialogResult = DialogResult.OK;
}

}

In Form 1 you can use this.

private void loansToolStripMenuItem_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    if(form2.ShowDialog() == DialogResult.OK)
    {
        textBox39.Text = form2.PassValue.ToString();
    }
}

I hope it will work for you.

Tien Nguyen Ngoc
  • 1,555
  • 1
  • 8
  • 17
0

Hi You are doing wrong thing, The testform and the Form1 that you are seeing are two different instance of the Form1. Obviously testform.textBox39 also different. To achieve this things you can make use of delegate and events as like the following:

Things in Form1

public delegate void ChangeText(string amountTxt);
public event ChangeText ChangeTextEvent;
private void loansToolStripMenuItem_Click(object sender, EventArgs e)
{
    ChangeTextEvent += new ChangeText(Change_Text)
    Form2 form2 = new Form2();
    form2.ChangeText = ChangeTextEvent;
    form2.ShowDialog();
 }

 public Change_Text(string amountTxt)
 {
     textBox39.Text = amountTxt;
 }

Things in Form2

public Delegate ChangeText;
private void button1_Click(object sender, EventArgs e)
{
    int tmp;
    Int32.TryParse(textBox2.Text, out tmp);
    ChangeText.DynamicInvoke(tmp.ToString()); // important line
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88