Share via

Cannot find reference using delegate-event mechanism

Rajkumar Rao 100 Reputation points
2023-10-15T20:26:48.3166667+00:00

Hi ,

i am stuck at a point where i am trying to reference of a control which is a panel type and which is in a form named as form1 .
below is the complete code

   internal class Operations
    {
        
        //paneldelegate part 
        public delegate void PanelAccessDelegate(Panel panel);

        public static event PanelAccessDelegate PanelAccessChanged;

        public static void UpdatePanel(Panel panel)
        {
            PanelAccessChanged?.Invoke(panel);
        }
    }


public partial class form1: Form
    {
        public form1() 
        { 
            InitializeComponent();
        }         
       private void form1_Load(object sender, EventArgs e) 
        {            
           Operations.UpdatePanel(this.panel1); // ok   
   openpanel.findpanel<Form2>();  // open form2 right away        
}    
 } 
}


 public static class openpanel
    {       

        // access panel 
       static Panel form1panel;
        public static void getpanelinstancehere(Panel param)
        {
            form1panel= param;
        }

        public static void findpanel<T>() where T : Form, new()
        {
            Operations.PanelAccessChanged += getpanelinstancehere;
           
            foreach (Form oldForm in form1panel.Controls.OfType<Form>().ToArray())

I am getting error at form1panel.controls . The error says that Object reference not set to an instance of an object.'

and when i check , the formpanel1 was null

Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

KOZ6.0 6,810 Reputation points
2023-10-15T20:53:07.3+00:00

The Operations.PanelAccessChanged event must be tied to openpanel.getpanelinstancehere before Operations.UpdatePanel is executed.

internal class Operations
{
    // ■ Add
    static Operations() {
        PanelAccessChanged += openpanel.getpanelinstancehere;
    }

    public delegate void PanelAccessDelegate(Panel panel);
    public static event PanelAccessDelegate PanelAccessChanged;
    public static void UpdatePanel(Panel panel) {
        PanelAccessChanged?.Invoke(panel);
    }
}

public static class openpanel
{
    static Panel form1panel;
    public static void getpanelinstancehere(Panel param) {
        form1panel = param;
    }

    public static void findpanel<T>() where T : Form, new() {
        // ■ Delete
        //Operations.PanelAccessChanged += getpanelinstancehere; 
        foreach (Form oldForm in form1panel.Controls.OfType<Form>().ToArray()) {
        }
    }
}

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.