I have multiple forms (e.g. Form1, Form2) that both contains a button that opens another form (Form3). In Form3 (pop-up form), the user is prompted to pick among the options, and once these were submitted through a button in Form3, the selected options will be transferred to the previous form where it was opened (either form1 or form2). Both forms1 and 2 are linked to one input form3, so im thinking of using a conditional statement upon clicking the "Submit" button in Form 3 that will determine whether the active form/currently maximized form is Form1 or Form2, and will let the program redirect and transfer the data accordingly to the specific form.
In maximized Form1 > clicks a button > Form 3 pop-up opens > User Input is submitted through a button > User Input is transferred to Form1
In maximized Form2 > clicks a button > Form 3 pop-up opens > User Input is submitted through a button > User Input is transferred to Form2
private void button1_Click(object sender, EventArgs e)
{
if (Form1.ActiveForm != null)
{
Form1.transfer.labQuan.Text = label8.Text;
double InitAmount, AmountwFee;
InitAmount = Convert.ToDouble(label12.Text);
AmountwFee = InitAmount + 100;
Form1.transfer.labAmount.Text = String.Format("P {0:N2}", AmountwFee);
this.Hide();
}
else if (Form2.ActiveForm != null)
{
Form2.transfer.labQuan.Text = label8.Text;
double InitAmount, AmountwFee;
InitAmount = Convert.ToDouble(label12.Text);
AmountwFee = InitAmount + 100;
Form2.transfer.labAmount.Text = String.Format("P {0:N2}", AmountwFee);
this.Hide();
}
}
It shows the output for Form1, but for Form2 there's no output. I tried placing Form2 in the first condition (if) and that works but not for Form1 this time. Apparently, what comes first is the only condition performed by the program, and the else if is not executed.
I tested if (Form1.Visible = true) works, but I've already tried and there was an error in the program. Should there be additional declarations or such or perhaps a new public class?
You're thinking about it backwards. Instead of "pushing" from Form3 back to Form1 or Form2, you should "pull" from Form3 directly from within the code in either Form1 or Form2. You can do this by showing Form3 with ShowDialog(), which will cause execution in the current form (Form1 or Form2j) to STOP until Form3 is dismissed. In Form3, you can make public properties that can be accessed to retrieve the values from it.
For example, here's a boiled down Form3:
Then in either Form1 or Form2, you'd do something like: