Calling one child form from another child form in C #

i has 3 forms: FormA, FormB and FormC, the form of which A is the mdiParent form, and the form B and C are children. I am writing the following code in a FormA load event.

private void frmMain_Load(object sender, EventArgs e)
{
   formB.MdiParent = this; //formB is instance of FormB             
   formC.MdiParent = this; //formC is instance of FormC
   formB.Show();      
}

what i want is when i press the button on FormB, FormC should be shown. now for this I will need to create another instance of FormC in the button click event in FormB or will I need to use the instancce created in FormA ???

if you need to create a separate instance, can someone explain the reason for this?

edit - the answer that Oded gave suits me. but can I make the return type of the property as Form [] to add more than 1 link, so that if I want to return from FormC to FormB, I can use a similar method?

also, if I want to transfer some data from FormB to FormC, then how can I do this?

+3
source share
6 answers

For your FormBreference is required FormC.

You can add a property to your own FromBfor this:

public Form FormCRef {get;set;}

Then in your main form:

formB.FormCRef = formC;

And in your class, FormBdo this in the event handler:

FormCRef.Show();
+2
source

You must use instances created in FormA because each instance of the form repeats a different form.

The right way to do this is to throw a FormB event, register with FormA, and then FormA can call whatever you want on FormC:

FormB:

// A delegate type for hooking up change notifications.
public delegate void MagicEventHandler();

public event MagicEventHandler MagicButttonClicked;    

// Invoke the event, this inside your button click event handler:
void Button1_OnClick(EventArgs e) 
{
    if (Changed != null) MagicButttonClicked();
}

FormA:  // ,   FormB formB;   FormB formC;

OnLoad...
{
    formB.MdiParent = this; //formB is instance of FormB             
    formC.MagicButttonClicked +=  new On_MagicButttonClicked ();
    formC.MdiParent = this; //formC is instance of FormC
    formB.Show();   
}

public void On_MagicButttonClicked()
{
    this.fromC.Activate();
}
+1

(, , ) , . - , , . Singleton.

C :

private static FormC thisForm = null;

public static FormC GetFormC()
{
  if (thisForm == null)
     thisForm = new FormC();

  return thisForm;
}

public static void ShowFormC()
{
  GetFormC().Show();
}

A, C:

FormC.GetFormC().mdiParent = this;

B, C, :

FormC.ShowFormC();

, , , - FormC ( , , , ), !

0

MessageBroker/EventBroker/EventAggregator.

:

:

public class ShowFormCEvent {}

in FormA:

EventPublisher.Register<ShowFormCEvent>(e=>formC.Show());

FormB

EventPublisher.Publish(new ShowFormCEvent());
0

:

FormB FormC, FormB FormC. , , , FormC FormA, FormB FormC. ( FormC FormB).

FormB FormC, FormB " " ( ) FormA, FormA FormC. :

class FormB
{
   ...
   private void SomethingHappened()
   {
      ((FormA)MdiParent).TellFormASomethingHappened();
   }

...

class FormA
{
   private FormC mFormC;

   ...

   public void TellFormASomethingHappened()
   {
      mFormC.TellFormCSomethingHappened();
   }

...

class FormC
{
    public void TellFormCSomethingHapened()
    {
       // do something
    }
...
0
source

In FormA (MDIForm):

FormB formB = new FormB();     
formB.MdiParent = this; 
formB.Show();

In FormB:

FormC formC = new FormC();
formC.MdiParent = (FormA)this.ParentForm;
formC.Show();
this.Close();
0
source

Source: https://habr.com/ru/post/1731164/


All Articles