开发者

Problem with Anchor property of controls inside UserControl

I've got this problem when creating a new custom UserControl. In details, I create a derived UserControl with a DataGridView that has Anchor property set to [Left, Top, Right, Bottom] so that the DataGridView can resize whenever the UserControl container's sizes change. But when the application runs, the result is not exactly expected. The DataGridView right- and bottom-edge always grow outside the UserControl border. To understand this issue, I have debugged and discovered that whenever a UserControl is created, its size is always default to [100, 100] despite we set it to [800, 600] or anything else. I think this is the cause of the problem. When we design the UserControl, we use a larger size but in runtime, its size is default to [100, 100] so the automatic calculation of Anchor property becomes unexpected.

Can anyone give me some hint o开发者_高级运维n how to solve this problem? I have one dirty solution but also sometimes malfunctions. My solution is that in UserControl's OnLoad event handler, I set the right and bottom-edge of child controls. This solution works fine for most of my custom UserControl but sometimes it makes me misunderstood. Sometimes the issue still happens. It makes me confused.

Can anyone give a clear explanation and a complete solution of this issue?

Thanks in advance


There are a couple ways you can resolve this issue, a quick and easy way is to put the GridView in an ASP PANEL(with scrollbars), and set the panel width to 100%. Now the Grid Size won't matter as you can scroll both Vertically and Horizontally.


The only solution I can find right now is to do an onload event with the User Control (sort of like you said) except, set the height and the width of the control that's malfunctioning relative to the parent User Control.... I have a datagridview and I'm setting it to Width - 50; and Height/2.5; This seems to stop it from out growing its parent, whilst still giving it the growing effects of anchoring. I think for whatever reason anchor makes it anchor to the window rather than parent User Control, which causes this bug.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜