Error adding panel to usercontrol

I have a problem. I am trying to access a panel in a custom control. When I access it in the form it works. Above. I did it.

I accessed a panel on a form from a custom control and it worked. Below is the code I used:

Form1 form = Application.OpenForms.OfType<Form1>().FirstOrDefault();
            form.Panel1.Controls.Clear();
            ManageControl user = new ManageControl();
            form.Panel1.Controls.Add(user);

      

But when I try to use the same concept in a custom control it doesn't work.

It throws a null error: "Object reference not set to object instance."

Below is the code:

//this is in ManageControl.cs
public Panel Panel2
        {
            get { return panelmanage; }
        } 

      

// this is in another userControl.Trying to access the panelImage

 ManageControl form = Application.OpenForms.OfType<ManageControl>().FirstOrDefault();
            form.Panel2.Controls.Clear();//it throws the error here
            ReportControl user = new ReportControl();
            form.Panel2.Controls.Add(user);

      

What am I doing wrong because I am using the same concept.

Thanks for the advanced one.

* edit This is my ManageControl.cs

 public partial class ManageControl : UserControl
    {

        public ManageControl()
        {
            InitializeComponent();

        }

        public Panel Panel2
        {
            get { return panelmanage; }

        }

      

This is how I am trying to access it in BookingListControl

public partial class BookingListControl : UserControl
    {
        ManageControl form = Application.OpenForms.OfType<ManageControl>().FirstOrDefault();
        public BookingListControl()
        {
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ManageControl form = Application.OpenForms.OfType<ManageControl>().FirstOrDefault();

            ReportControl user = new ReportControl();
            form.Panel2.Controls.Add(user);
        }

      

+3


source to share


2 answers


ManageControl

is UserControl

not a Form

. So when you look for open type forms ManageControl

you get nothing (no surprises here). Then we call FirstOrDefault

, it returns null (since there were no matching items in the empty collection) and your next line explodes.

This approach is doomed to begin with, because even if you had a whole bunch of shapes overlapping and able to make it work (bad idea), it would break when you had two objects ManageControl

and needed to access the second ...

Instead, first ask yourself, "Why do my UserControls need to refer to each other?" This is an important question, because they are generally UserControl

independent. They probably have ways to return the data to their parents, but they do. They certainly don't interact with other UserControls.

If you decide that you really need this dependency, I will pass the parent Form

for both the UserControl

public property in Form

, which will allow them to see the other UserControl

. From here you can access it normally (no bullshit needed OpenForms

). To be honest, this is a massive code smell, and it looks like the whole design needs to be reviewed to see where you have dependencies to remove.

To do this, you need to open ManageControl

in the form:



public class ParentForm : Form
{
    public ManageControl Manager { get { return manageControlInstance; } }
    ...
}

      

Then go into your child control. The easiest way is through a property Parent

, but you can also pass it in a constructor or function Init

.

public class ChildControl : UserControl
{
     private void SomeFunction()
     {
         (Parent as ParentForm).Manager.Panel2.Controls.Add(new ReportControl());
     }
}

      

The code is pretty ugly and I wouldn't recommend it (it's also unsafe if you put it ChildControl

in something other than ParentForm

). This says it will work.

+1


source


Assuming a NullReferenceException occurred based on the Panel2 property access, your problem is that "panelmanage" is null. Is the code that finds and populates "form.Panel2" in a form or control constructor? If so, try rebuilding it to run after the ManageControl is fully initialized - perhaps by placing it in the Loaded event.



0


source







All Articles