Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to get 2 textboxes text in UserControl3 to be added into a listbox in UserControl4 upon clicking a button in UserControl3. It works if both the UserControls are seen together in the form.

But when I do it seperately like a tab control function where if you click btn1 it opens UserControl3, btn2 opens UserControl4 in a single panel. It wouldn't work, it will come up with an error that says :

System.NullReferenceException: 'Object reference not set to an instance of an object. 
   
control1 was null.


I'm not too sure what it means or how to exactly fix it?
But here is the code I have so far:

UserControl3
public partial class UserControl3 : UserControl
{
        public UserControl3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var control1 = Parent.Controls.OfType<UserControl4>().FirstOrDefault();
            control1.TheListBox.Items.Add(textBox1.Text + ", " + textBox2.Text);
        }
}


UserControl4

What I have tried:

I'm not too sure what it means or how to exactly fix it?
But here is the code I have so far:

UserControl3
public partial class UserControl3 : UserControl
{
        public UserControl3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var control1 = Parent.Controls.OfType<UserControl4>().FirstOrDefault();
            control1.TheListBox.Items.Add(textBox1.Text + ", " + textBox2.Text);
        }
}


UserControl4
public partial class UserControl4 : UserControl
{
        public ListBox TheListBox
        {
            get { return listBox1; }
            set { listBox1=value; }
        }

        public UserControl4()
        {
            InitializeComponent();
        }

}
Posted
Updated 31-Mar-22 0:31am

FirstOrDefault will return null if there are no matching elements.

If you are expecting that there may not be a matching element, then you need to check the return value before trying to use it.

If you believe there should always be a matching element, then you need to use First instead, which will throw a "sequence contains no elements" error if there are no matching elements. If you encounter that error, you will then need to debug your code to find out why your expectation was not met.

However, this code seems like a really bad idea to me. User controls should not know anything about their parent control, and especially not any other controls within the same parent. One of our regulars has written a series of articles on how to do this properly:

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
 
Share this answer
 
Comments
Allysha April 31-Mar-22 5:18am    
Is the article most about forms? because I am trying to create somewhat of a Tab Control feature instead of having another form having to be open
Richard Deeming 31-Mar-22 5:20am    
The techniques and advice apply to controls within a form, not just forms. :)
Allysha April 31-Mar-22 5:25am    
Ahh okay, thank you. I will look into and try to get it working. Thank you
The message means that the following line failed:
C#
var control1 = Parent.Controls.OfType<UserControl4>().FirstOrDefault();

For whatever reason, nothing was returned by the call to FirstOrDefault. You need to use the debugger to find out why.

Also your use of the default names generated by Visual Studio is considered bad form. Calling everything UserControlX, ButtonX etc, can lead to very confusing code.
 
Share this answer
 
Comments
Allysha April 31-Mar-22 5:12am    
Thank you for the solutions and advices. This is just an example of the actual code application I am trying to create. I will look into the things told here
Don't do it like that - it's bad design from an OOPs standpoint, because it "locks" the design of your parent form / control and the UserControl4 together, so UserControl3 can't be reused and neither of the locked objects can have their design changed without considering the impact that might have on other parts of you app, or even other apps that might try to reuse your assembly later.

Instead, go via the parent and let it make the decisions: Transferring information between two forms, Part 3: Child to Child[^] show you how. It talks about Forms, but Forms are derived from Control and it's exactly the same process - and code - for UserControls.
 
Share this answer
 
Comments
Allysha April 1-Apr-22 0:02am    
I read and tried what was in the article today, it does work if the user controls are seen on the main form simultaneously. When I put it as a tab control function where only 1 usercontrol is seen at a time, it will not work. Keeps giving me the same error of:

System.NullReferenceException: 'Object reference not set to an instance of an object.

I'm not sure how to go about that...
OriginalGriff 1-Apr-22 2:06am    
Use the debugger to find out exactly what is null - then work back from that to find out why.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900