Problems after adding Control dynamically on Panel
I have added an View List onto a Panel like this:
panelComponent.Controls.Add(viewListC开发者_如何学Pythonomponent);
Everything works just fine. Mouse events are handled, repainting works. But one problem: I can't move it around dynamically. If I change the control.Top
variable, it just sits there without moving.
It's like the control is glued to the top left corner. Resizing the right and bottom properties works just fine! I did it without dynamically adding and then no problem.
What could be causing this, and how do I fix it?
Two possible explanations. First is the Dock property, docking it to the top keeps the control at the top of the container, no matter what you assign to the Top or Location properties.
The other one is value types, the Location property is Point, a struct. This code will not move the control:
var lbl = new Label();
panel1.Controls.Add(lbl);
var pos = lbl.Location;
pos.Y = 42; // No effect
Try using the Location
property:
viewListComponent.Location = new Point(42, 42);
精彩评论