开发者

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?开发者_如何学JAVA I've tried moving the ListView to the Form (in order to rule out weird behaviour when docked in a splitcontainer) to no avail.


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.


  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?


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜