Home > Enterprise >  Calling mehtod from another form / user control nothing is happening while on parent form its workin
Calling mehtod from another form / user control nothing is happening while on parent form its workin

Time:03-24

I have a parent form that loads different user controls but when I am trying to access a method on the parent form from a button on the user controller which is not working but if the same method is accessed from the own parent form its all good I excluded the rest of code and here is the code not working

method on Parent Form:

using IT_HUB_MANAGEMENT_SOLUTION.SERVICES;

namespace IT_HUB_MANAGEMENT_SOLUTION.APP_FORMS
{
    public partial class FIRST_RUN_FRM : Form
    {
        public FIRST_RUN_FRM()
        {
            InitializeComponent();
        }

 public void NEXT_STEP_CMD()
        {
            MAIN_PANEL.Controls.Clear();
            FIRST_RUN_OBJECTS.CRYSTAL_REPORTS_CONTROL cRYSTAL_REPORTS_CONTROL = new();
            cRYSTAL_REPORTS_CONTROL.Dock = DockStyle.Fill;
            MAIN_PANEL.Controls.Add(cRYSTAL_REPORTS_CONTROL);
        }
}

here is the code on user controls:

public partial class DOTNET_INSTALLER_CONTROL : UserControl 
    {       

        public DOTNET_INSTALLER_CONTROL()
        {
            InitializeComponent();
    }
 private void NEXT_BTN_Click(object sender, EventArgs e)
        {
            FIRST_RUN_FRM fff = new();
            fff.NEXT_STEP_CMD();
        }
}

CodePudding user response:

First of all, this is not a normal behavior (at least for me) that a UserControl calls a parent form method directly. Usually user controls are not aware of the parent that is using them, and for me, it's a really smelly code.

In your code:

public partial class DOTNET_INSTALLER_CONTROL : UserControl 
{       
    public DOTNET_INSTALLER_CONTROL()
    {
        InitializeComponent();
    }
    
    private void NEXT_BTN_Click(object sender, EventArgs e)
    {
        FIRST_RUN_FRM fff = new();
        fff.NEXT_STEP_CMD();
    }
}

You're simply creating a new instance of the FIRST_RUN_FORM (Also try to follow the naming convention) and then calling it's function. This won't throw a compiler error since it's a valid .Net syntax. You're creating a new class and calling a public function inside it. However, it won't show anything on your initial form because it's not the same instance.

There's two ways to fix this code, and I don't like the first way I'll show you.

  1. You can add the parent form instance to the user control and pass it on when creating a new instance of the UC. Something like this:

     public partial class DOTNET_INSTALLER_CONTROL : UserControl 
     {
         private FIRST_RUN_FRM _parent = null;
    
         public DOTNET_INSTALLER_CONTROL(FIRST_RUN_FRM parent)
         {
             InitializeComponent();
             _parent = parent;
         }
    
         private void NEXT_BTN_Click(object sender, EventArgs e)
         {
             if (_parent == null) 
             {
                 return;
             }
    
             _parent.NEXT_STEP_CMD(); //This might throw an error not related to this question
         }
     }
    

I wouldn't recommend this though.

What I'd go for is using events, this way you don't need the UserControl to know who their parent is or what should be done to the parent when a button inside the UC is clicked.

  1. Event:

In the user control:

public partial class DOTNET_INSTALLER_CONTROL : UserControl 
{
    public event EventHandler NextButtonClicked;
    
    public DOTNET_INSTALLER_CONTROL(FIRST_RUN_FRM parent)
    {
        InitializeComponent();
        _parent = parent;
    }
    
    private void NEXT_BTN_Click(object sender, EventArgs e)
    {
        OnNextButtonClicked();
    }
    
    protected virtual void OnNextButtonClicked()
    {
        EventHandler handler = NextButtonClicked;
        handler?.Invoke(this, null);
    }
}

And in the parent form you'll subscribe to this event and react when it's fired.

  • Related