Bigboss
Bigboss

Reputation: 345

Calling a function located in a parent user control from a child user control in winform

This is my parent form:

 public partial class ParentControl: UserControl
 {

    public ParentControl()
    {
        InitializeComponent();   
    }

     private void button1_Click(object sender, EventArgs e)
    {
        ChildForm child= new ChildForm ();
        child.Dock = DockStyle.Fill;
        TabPage tabNewChild= new TabPage("Child");
        tabNewChild.Controls.Add(child);

        tabDetails.TabPages.Add(tabNewChild);
        tabDetails.SelectedIndex = tabDetails.TabPages.IndexOf(tabNewChild);
    }
     void CloseTab()
     {
          \\Close the selected tab
     }
}

This is my child usercontrol:

public partial class ChildForm : UserControl
{
    public ChildForm ()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
         \\Call the CloseTab in parent user control.
    }
}

What is the optimal and proper way of implementing this?

I have researched about delegates and eventargs but cant decide what to use.

I have lots of modules that will be implemented in this way thats why I want to know the proper way of doing it. THanks a lot.

Upvotes: 1

Views: 1280

Answers (2)

Sefe
Sefe

Reputation: 14017

A good solution is to create an event on your user control that is triggered when a close is requested:

public partial class ChildForm : UserControl
{
    public ChildForm ()
    {
        InitializeComponent();
    }

    public event EventHandler CloseTabRequested;

    protected virtual void OnCloseTabRequested(EventArgs e)
    {
         CloseTabRequested?.Invoke(this, EventArgs.Empty);
    }

    private void button1_Click(object sender, EventArgs e)
    {
         OnCloseTabRequested(EventArgs.Empty);
    }
}

You can handle the event in the parent form:

 public partial class ParentControl: UserControl
 {

    public ParentControl()
    {
        InitializeComponent();   
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ChildForm child= new ChildForm ();
        child.Dock = DockStyle.Fill;
        child.CloseTabRequested += ChildForm_CloseTabRequested;
        TabPage tabNewChild= new TabPage("Child");
        tabNewChild.Controls.Add(child);

        tabDetails.TabPages.Add(tabNewChild);
        tabDetails.SelectedIndex = tabDetails.TabPages.IndexOf(tabNewChild);
    }

    void ChildForm_CloseTabRequested(object sender, EventArgs e)
    {
        CloseTab((ChildForm)sender);
    }

    void CloseTab(ChildForm requestingForm)
    {
        \\Close the selected tab
    }
}

With this solution the user control is not bound to a specific parent form for maximum reusability. It also avoids a dependency of the child form on the parent form, which is good design.

Upvotes: 1

user12031933
user12031933

Reputation:

You can access to the Parent property of the ChildForm then cast it to ParentControl and call the CloseTab method:

public partial class ChildForm : UserControl
{
  private void button1_Click(object sender, EventArgs e)
  {
    (Parent as ParentControl)?.CloseTab(this);
  }
}

You may add the tab instance as method argument to close the good tab.

public partial class ParentControl: UserControl
{
  public void CloseTab(ChildForm sender)
  {
    // close sender
  }
}

Upvotes: 1

Related Questions