From one form, another one is launched with the help of a similar code (in fact, a standard one):

private void Show_Click(object sender, EventArgs e) { Child form1 = new Child(); form1.ShowDialog(); } 

How to find out the type of the form that launched Child ? Based on this, the contents of the form Child should be determined.

  • one
    Try to stick to the one-way flow of information: the form / class that Child creates knows about Child , and Child does not know about the form / class that creates it. What happens if Child is created in a class method that is not a form? Submit to the constructor of the Child class, or set as its property, an indicator of the behavior of Child . - Igor
  • @Igor, i.e., you suggest to just send some. information in Child , and when you initialize Child 'and use it? - Byulent
  • Yes, I offer it :) - Igor
  • They tell you Show_Click , and look, you have a Show_Click method in which the object and the event arguments are passed as parameters. If you really need, you can pass data into your form through a constructor, and then use it like this: Child form1 = new Child(MyDataStruc Data); where MyDataStruc is a structure / class with the data you need. Or simply pass the link to the parent. But here, too, has its own nuances. - BlackWitcher
  • And there is such a link to the article. There, paragraph 2 considers the transfer of data from one form to another. As many as 7 ways. Both pros and cons are indicated. So, if you have parent information in the child form, then your task is solved, therefore. - BlackWitcher

2 answers 2

Well, once @VladD considers a shameful answer with a flashback :)

Try to stick to the one-way flow of information: the form / class that Child creates knows about Child , and Child does not know about the form / class that creates it.

(What happens if Child is created in a class method that is not a form? Think about it.)

Submit to the constructor of the Child class, or set as its property, an indicator of the behavior of Child .

    No

    In C #, there is no legal method to find out who caused the code. Even if it was possible, it is a bad practice: it is not amenable to refactoring or testing.

    Let the code showing the form, you configure it yourself, passing it the necessary information. In the worst case, it could be just this , at best - a specialized data structure describing what needs to be shown.

    An even better option is to take out the business logic (under what circumstances a new window should be opened, and what information should be shown in it) from the UI code (source code code), but this will most likely require a lot of processing of the project.