Leo Vo
Leo Vo

Reputation: 10330

Find a design pattern in this situation

I am developing a .NET WinForm application. I have a controller A, this controller A will manage creating a list of forms: FormA1, FormA2, ... And I have a controllerB, this controller B will manage creating a list of forms: FormB1, FormB2.

Each form will be opened like a tab in a MainForm which user can choose a tab to show a form in that tab.

When user choose FormA1, I can know it is from controller A, or when user choose FormB1, I can know it is from controller B.

I want to know the way to help me to find the controller corresponding Form which user open. Give me the best way and the design pattern I should use.

Thanks.

Upvotes: 0

Views: 166

Answers (2)

Anders Forsgren
Anders Forsgren

Reputation: 11111

Are the controllers two different types (classes)? Are the two kinds of forms different types (classes)? In that case, just make a field where you pass the controller to the form upon creation

interface IController
{
    ControllerForm CreateForm();
}

class ControllerA : IController
{
    public ControllerForm CreateForm()
    {
        return new FormA(this);
    }
}

class ControllerB : IController
{
    public ControllerForm CreateForm()
    {
        return new FormA(this);
    }
}

abstract class ControllerForm : Form 
{
   public IController Controller { get; private set; }

   protected ControllerForm(IController controller)
   {
       this.Controller = controller;
   }
}

class FormA : ControllerForm 
{
    public FormA(IController controller)
      : this(controller)
    {
    }
}

class FormB : ControllerForm 
{
    public FormB(IController controller)
      : this(controller)
    {
    }
}

Upvotes: 1

Jaapjan
Jaapjan

Reputation: 3385

It has been a long time since I did WinForms programming-- but I do recall every component has Tag field. You can set the tab's tag field to your controller. When something is done, refer to the active tab, get the controller from the tag field and do something.

Upvotes: 1

Related Questions