Home > Net >  How to let the program detect the previous form where a new form was accessed so that the input from
How to let the program detect the previous form where a new form was accessed so that the input from

Time:12-05

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?

CodePudding user response:

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:

public partial class Form3 : Form
{

    public Form3()
    {
        InitializeComponent();
    }

    public String LabQuantity
    {
        get
        {
            return label8.Text;
        }
    }

    public double AmountwFee
    {
        get
        {
            return Convert.ToDouble(label12.Text)   100;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
    }

}

Then in either Form1 or Form2, you'd do something like:

private void button1_Click(object sender, EventArgs e)
{
    Form3 f3 = new Form3();
    if (f3.ShowDialog() == DialogResult.OK) // code STOPS here until "f3" is dismissed!
    {
        // ... do something with the data from "f3" ...
        Console.WriteLine(f3.LabQuantity);
        Console.WriteLine(f3.AmountwFee);
    }
}

CodePudding user response:

Thank you for all the suggestions! I'm learning a lot from the feedback because I'm only new to this as a student. Since coding is still very complicated for me, I decided to learn how to duplicate forms and proceeded to make a separate form3 for both form1 and form2. They were the common "copy, paste" techniques plus the renaming of forms, but it worked well in the end.

I will try to implement the suggestions the next time we have a coding assignment. Thank you!

  • Related