Tutorial :WinForms: Why is Control.Parent null?



Question:

I'm trying to get the parent of a listview docked within a splitcontainer, and am finding that ListView.Parent is null.

According to the documentation this should be:

A Control that represents the parent or container control of the control.

Can anyone explain why this property would be null? I've tried moving the ListView to the Form (in order to rule out weird behaviour when docked in a splitcontainer) to no avail.


Solution:1

This was entirely my mistake unfortunately. Rather than delete my question in shame, I'm going to say what I did wrong in case anyone else does the same.

Essentially, my code was running as a result of the ListView.Disposed event. This event presumably happens after the control has been removed from it's parent's controls collection, which is why Parent was null.


Solution:2

  1. Is the listview in the controls collection of the splitcontainer?
  2. When are you checking the parent property? Can you override the OnLoad method and check after base.OnLoad is called?


Solution:3

The ListView seems not to be in the control collection of any SplitContainer's SplitterPanel (designer-bug?). When it doesnt work on the designer change it in the designer.vb(?) manually like this:

Me.SplitContainer1.Panel2.Controls.Add(Me.ListView1)  


Solution:4

Are you sure you are using the correct listView? If it is appearing in a winforms app, I think it has to have a parent. Otherwise, it wouldn't get rendered, since that starts at the form and recursively hits children. If you have reference to a newly constructed listview that you haven't actually used yet, the parent could be null.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »