locked
C# .NET WinForms Calling Form method from usercontrol RRS feed

  • Question

  • I'm writing a C# WinForms app with the following arrangement/control hierarchy...

    Form =>TabControl =>TabPage =>UserControl

    On Form, I add a new tab page to an existing TabControl, then I add a new UserControl to the tab page.

    Here's the code for that from Form...

     
           public void NewCategory()
            {
                tbctrlCategories.TabPages.Add(" Category " + (tbctrlCategories.TabCount + 1).ToString());
                clsCategory ThisCat = new clsCategory();
                ThisCat.intTotalCatQuestions = 0;
                ThisCat.strCatName = tbctrlCategories.TabPages[tbctrlCategories.TabCount - 1].Text.ToString();
                ucCategory ucCat = new ucCategory(null, ThisCat, glbMCQ2BCopied, glbblAudioDeviceFound);
                this.Controls.Add(ucCat);
                tbctrlCategories.TabPages[tbctrlCategories.TabCount - 1].Controls.Add(ucCat);
                ucCat.Parent = this;
                ucCat.Dock = DockStyle.Fill;
                ucCat.Show();
            } 

         

    At some point, I need to call a method on Form from the UserControl. Here's the code for that from the UserControl (ucCategory)...

     
           public void ProjectNeedsSavingNotification()
            {
                frmMultiCategoryProject frmMC = (frmMultiCategoryProject)this.Parent;
                if (frmMC.InvokeRequired)
                    frmMC.Invoke(new Action(() =>
                    {
                        frmMC.ProjectNeedsSavingNotification();
                    }));
                else
                {
                    frmMC.ProjectNeedsSavingNotification();
                }
            }



    Problem: On the first line in ProjectNeedsSavingNotification(), the app throws a null reference indicating that frmMC is null. I'm guessing there's a missing link somewhere between creating a new tab page on the TabControl and creating the UserControl on the new tab page. In ProjectNeedsSavingNotification(), I need to call a method in Form. How can I accomplish that?

    Friday, November 10, 2017 12:16 AM

Answers

  • Most experienced programmers would recommend avoiding making the UserControl dependent on its users; in other words, depending on the tab page, the tab control or the form. A better design would be to create a delegate in the UserControl that is like a callback. The UserControl can call the delegate if it is set. If the call must go all the way to the form then you need to do the same for the form and the tab page; probably there is no need for the tab control to be involved.


    Sam Hobbs
    SimpleSamples.Info

    Friday, November 10, 2017 5:35 PM

All replies

  • Most experienced programmers would recommend avoiding making the UserControl dependent on its users; in other words, depending on the tab page, the tab control or the form. A better design would be to create a delegate in the UserControl that is like a callback. The UserControl can call the delegate if it is set. If the call must go all the way to the form then you need to do the same for the form and the tab page; probably there is no need for the tab control to be involved.


    Sam Hobbs
    SimpleSamples.Info

    Friday, November 10, 2017 5:35 PM
  • Thanks, Sam. I'll try the delegate.
    Monday, November 13, 2017 11:14 PM
  • If you need help with sample code then I can help. Delegates can be confusing in the beginning. There are plenty of examples available if you need help but I can try to help too.


    Sam Hobbs
    SimpleSamples.Info

    Monday, November 13, 2017 11:21 PM