-3

I have Form1 and in that form, i have this code

Form2 frm = new Form2();
frm.ShowDialog();

So now is my question: how to know if form2 is called by the way like that
in Button event in form2?
In button event in form2, I want to check if ShowDialog() is called FROM FORM1 (NOT FROM ANOTHER FORM), if button is clicked, form2 is closed!

Jacky Euro
  • 69
  • 1
  • 8
  • 1
    Add just a parameter to the constructor of `form2` or set a property (or a method) in `form2` – akop Sep 25 '18 at 07:40
  • can u type example code? i'm don't know how to do as you say – Jacky Euro Sep 25 '18 at 07:47
  • Look at my answer. – akop Sep 25 '18 at 07:55
  • This may be a duplicate (or close to it): [How to get reference to the Calling Form from function without parameter](https://stackoverflow.com/q/46366099/608639) – jww Sep 25 '18 at 08:11
  • Though the question is of poor quality, I think I managed to provide a correct solution. See my answer. – jegtugado Sep 25 '18 at 08:12
  • I think you have a usability problem. If the caller was not the desired form, the button should be either disabled of invisible. – Cleptus Sep 25 '18 at 08:19
  • @bradbury9: Form2 would still probably need to know the form from which it was called to disable/hide the button. – PaulF Sep 25 '18 at 09:57
  • Then, if it is not used in the constructor or the form, checking the property `Owner` would be OK https://stackoverflow.com/a/38469261/2265446 – Cleptus Sep 25 '18 at 11:58

4 Answers4

2

You can use Form.Owner Property.

Form1:

Form2 frm = new Form2();
frm.ShowDialog(this); // owner parameter

Form2:

if (this.Owner != null)
{ 
    // Owner is not null, there is a calling form
    // Do something
    if (this.Owner is Form1)
    {
       Form1 form1 = (Form1)this.Owner; // Form1 called this form!
    }
}
jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • Take in mind it should not be checked in the constructor of the form, because it would not be asigned yet, refer https://stackoverflow.com/a/38469261/2265446 – Cleptus Sep 25 '18 at 11:59
  • In OP's case he would want to use this in his button click event. – jegtugado Sep 26 '18 at 00:59
1

Please try to make more precise what you are asking.

public partial class Form2: Form
{   
    public static bool wasCalledFromForm1 = false;
    public Form2 (bool form1Called = false)
    {
        InitializeComponent();
        wasCalledFromForm1 = form1Called;
    }
    private void Button1_Click(object sender, System.EventArgs e)
    {
        if (wasCalledFromForm1) this.Close;
    }
}

Button 1 to open Form2:

Form2 form2 = new Form2(true);
form2.ShowDialog();
julian bechtold
  • 1,875
  • 2
  • 19
  • 49
0

From what I understand you want to know if there is a way to check if the showDialog has been called successfully or not, for that you can use the following code snippet:-

Form2 frm = new Form2();
var result = frm.ShowDialog();
if (result == DialogResult.OK)
{
    // apply your logic
}

PS:- In future please compose your questions properly and carefully so that its easier for us to answer :) :) :)

p0ison c0de
  • 139
  • 5
  • This is not a correct answer - OP would have to write code to ensure the form returns that value. ShowDialog can return several different values & still have been called successfully - for example, if the form is closed by clicking the top left close button (as many people will do) the default return value is DialogResult.Cancel not DialogResult.OK. – PaulF Sep 25 '18 at 08:06
  • thanks for pointing that out @PaulF, since the question was not clear I didn't go to full length and check all the conditions, I was expecting some comment from the person asking the question, anyways I will retract or modify the answer once the question has been modified so that it can be understood properly :) – p0ison c0de Sep 25 '18 at 09:10
0

A example with constructor.

form2 frm2 = new Form2(calledByFrm1: true);
frm2.ShowDialog();

// ...

class Form2 ... 
{
    boolean calledByForm1;

    public Form2(boolean calledByForm1) 
    {
        this.calledByForm1 = calledByForm1;
    }


    public Form2_Onload .... 
    {
        if (this.calledByForm1) 
        {
            // your logic here
        }
    }
}
akop
  • 5,981
  • 6
  • 24
  • 51