-2

I have a tried my best to create a dummy project as an example for this question. In my Case i have multiple UserControls That i load into my main Form. What I am Trying to do is I have defined a Method ShowControl() in main form that adds usercontrols to the panel.

public void Showcontrol(UserControl ctrl)
        {
            if (!pnl_ucContainer.Controls.Contains(ctrl))
            {
                pnl_ucContainer.Controls.Add(ctrl);
                ctrl.Dock = DockStyle.Fill;
                ctrl.BringToFront();
            }
            else
            {
                ctrl.BringToFront();
            }
        }

Let's Leave this code for a while and have a look at my UserControl Structure. What I have done is I have Used a Traditional Practice to implement Singleton Design Pattern. Like The Following for each UserControl in My Project.

namespace DemoApp
{
     public partial class uc_HomePage : UserControl
{
    static uc_HomePage _obj;

    public static uc_HomePage GetInstance
    {
        get
        {
            if (_obj == null)
            {
                _obj = new uc_HomePage();
            }
            return _obj;
        }
    }

    public uc_HomePage()
    {
        InitializeComponent();
    }
    }
}

To Call It From a Form1 Control Event I have this Simple Code.

private void Helper_Click(object sender, EventArgs e)
        {
            Showcontrol(uc_HomePage.GetInstance);
        }

WHich Works For Me! As You Can see in the Following Image

Meanwhile When I want to add Another UserControl to the Same Panel and Bring it to Front I want to trigger the ShowControl() Method From the uc_HomePage UserControl. Like Following.

private void uc_Button_Click(object sender, EventArgs e)
        {
            // Since Form1 is a Singleton Class
            // I will call the method like this.

            Form1.GetInstance.Showcontrol(uc_SettingsPage.GetInstance);
        }

Well Nothing Happens When I call it from a usercontrol Button.

Please Help Me.....

EDIT: WHy The Down Votes Can Anyone Explain so that i can improve

  • You're saying "... I want to trigger the ShowControl() Method From the uc_HomePage UserControl", but your HomePage control doesn't provide a ShowControl() method. Is the ShowControl() on the form or on each control? Also the code for your "Form.GetInstance" is not shown. – Sven Bardos Aug 18 '19 at 17:37
  • ShowControl() is a Method defined in form I want to it from The UserControl........ ``public partial class Form1 : Form { static Form1 _obj; public static Form1 GetInstance { get { if (_obj == null) { _obj = new Form1(); } return _obj; } } public Form1() { InitializeComponent(); } }`` – Ahmed Faraz Aug 19 '19 at 07:28

1 Answers1

0

What I have done is I have Used a Traditional Practice to implement Singleton Design Pattern.

That's not how it's done. First, Singleton is an anti-pattern and you should think very hard before using it anywhere. If in doubt... don't. The things that you thought were a good solution to your problem are actually the side-effects of the singleton pattern that everybody agrees on are bad, but a necessary evil if you want the advantages.

The problems you are trying to solve are normally solved with a pattern of the M* group (MVC, MVVM, MVP etc) and probably the help of dependency injection frameworks.

I'm sorry that I don't have a practical, technical solution to your current problem. I'm sure there is one. But that would be like giving you painkillers without a cure. It makes the symptoms appear weaker, but it does not help you with the real problem.

I cannot tell you which M* pattern to pick. You should get a good book (or tutorial, I prefer real books) and read up on it. Don't follow down the Singleton rabbit hole, the longer you stay on that course and duct-tape it to make it work, the more work it will be to undo it and follow a proper UI pattern.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Thanks @nvoigt. I 'm glad that you paid some attention to my problem. Now that I have made my mind that traditional Singleton Pattern Is preventing me to achieve my goal....I would try to tackle this problem with a new approach.......May be From Scartch...... – Ahmed Faraz Aug 19 '19 at 08:01