Neil Barnwell
Neil Barnwell

Reputation: 42125

WinForms: Why is Control.Parent null?

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.

Upvotes: 0

Views: 1999

Answers (4)

Neil Barnwell
Neil Barnwell

Reputation: 42125

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.

Upvotes: 2

cdkMoose
cdkMoose

Reputation: 1636

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.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460108

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)

Upvotes: 0

Thomas
Thomas

Reputation: 8003

  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?

Upvotes: 0

Related Questions