0

So I basically have a form that is opened and I need to check what form called that one to open

"Parent form"

    private void btnEnter_Click(object sender, EventArgs e)
    {
        this.Close();
        newForm nf = new newForm();
        nf.Show()
    }

"Opened form"

    private void newForm_Load(object sender, EventArgs e)
    {
        if parent is ("oldForm") // Need to know how to code for this line.
        {
           //do some stuff here 
        }

        else
        {
           //Do something different
        }
    }

So for example if oldForm was the form that called this one, something specific would happen that wouldn't if "anotherForm" called it for example

MistaOmega
  • 30
  • 5
  • 3
    Wouldn't it be easier to pass a value to the child form which tells it what to do on load? – Esko Nov 07 '18 at 13:48
  • There are many ways, e.g. [checking stack](https://stackoverflow.com/q/171970/1997232), but a simple constructor overload with parameter will do the best. – Sinatr Nov 07 '18 at 13:52
  • As in just passing a variable over and checking it on the new form?, potentially yeah :D – MistaOmega Nov 07 '18 at 13:55

1 Answers1

-1

You could just add a 'Parent' property to your forms, and set it before calling Show.

private void btnEnter_Click(object sender, EventArgs e)
{
    this.Close();
    newForm nf = new newForm();
    nf.Parent = this;
    nf.Show()
}

Your form would look like this:

public class MyForm
{
    public Form Parent {get;set;} 

    private void newForm_Load(object sender, EventArgs e)
    {
        if (this.Parent is oldForm) 
        {
           //do some stuff here 
        }
        else
        {
           //Do something different
        }
    }
}

Note that if (this.Parent is oldForm) is equivalent to if (this.Parent.GetType() == typeof(oldForm))

As one of the comments said, if you're only using the Parent property to make this one decision, you'd be better to define it as a Boolean property, called DoSomething that indicates what it does. Combining this with the other suggestion gives:

public class MyForm
{
    private bool specialMode;

    public MyForm(bool mode)
    { 
        this.specialMode = mode;
    }

    private void newForm_Load(object sender, EventArgs e)
    {
        if (this.specialMode) 
        {
           //do some stuff here 
        }
        else
        {
           //Do something different
        }
    }
}

which you'd call like this:

private void btnEnter_Click(object sender, EventArgs e)
{
    this.Close();
    newForm nf = new newForm(true); // SpecialMode = ON
    nf.Show()
}
Robin Bennett
  • 3,192
  • 1
  • 8
  • 18
  • Nice simple idea, but I would do that as a constructor parameter, so that there is no way of forgeting about it (if it happens to be mandatory). – Alejandro Nov 07 '18 at 13:50
  • Ah okay, I see, but what would I do with that when I made it to the next form, as in, how would I use the fact I declared a parent? – MistaOmega Nov 07 '18 at 13:54
  • With a `this.Close();` before `newForm nf = new newForm();`, what happens to `Parent` after? Maybe, just pass the Parent name, as a string, in another way (in the `newForm` constructor, maybe)? – Jimi Nov 07 '18 at 14:13
  • 'this.Close()' only stops the form being displayed, it exists until nothing references it. – Robin Bennett Nov 07 '18 at 14:15
  • The `Control` class that `Form` is derived from already has a `Parent` [property](https://learn.microsoft.com/en-gb/dotnet/api/system.windows.forms.control.parent?view=netframework-4.7.2) – Handbag Crab Nov 07 '18 at 14:38