UserControl 内控件的 Anchor 属性存在问题

发布于 2024-08-15 06:05:19 字数 607 浏览 3 评论 0原文

创建新的自定义用户控件时我遇到了这个问题。具体来说,我创建了一个带有 DataGridView 的派生 UserControl,该 DataGridView 的 Anchor 属性设置为 [Left、Top、Right、Bottom],以便只要 UserControl 容器的大小发生变化,DataGridView 就可以调整大小。但当应用程序运行时,结果并不完全符合预期。 DataGridView 右边缘和下边缘始终超出 UserControl 边框。为了理解这个问题,我进行了调试,发现每当创建 UserControl 时,它的大小始终默认为 [100, 100],尽管我们将其设置为 [800, 600] 或其他任何值。我认为这就是问题的原因。当我们设计UserControl时,我们使用了更大的尺寸,但在运行时,它的尺寸默认为[100, 100],因此Anchor属性的自动计算变得意外。

谁能给我一些关于如何解决这个问题的提示?我有一种肮脏的解决方案,但有时也会出现故障。我的解决方案是在 UserControl 的 OnLoad 事件处理程序中,我设置子控件的右边缘和下边缘。这个解决方案适用于我的大多数自定义用户控件,但有时它会让我产生误解。有时该问题仍然会发生。这让我很困惑。

谁能给这个问题一个明确的解释和完整的解决方案?

提前致谢

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 on 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可可 2024-08-22 06:05:19

有几种方法可以解决此问题,一种快速且简单的方法是将 GridView 放入 ASP 面板(带滚动条)中,并将面板宽度设置为 100%。现在,网格大小不再重要,因为您可以垂直和水平滚动。

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.

若水微香 2024-08-22 06:05:19

我现在能找到的唯一解决方案是使用用户控件执行 onload 事件(有点像您所说的),除了设置相对于父用户控件发生故障的控件的高度和宽度......我有一个 datagridview,我将其设置为 Width - 50;和高度/2.5;这似乎阻止了它超越其父母,同时仍然赋予它不断增长的锚定效应。我认为无论出于何种原因,锚点都会使其锚定到窗口而不是父用户控件,这会导致此错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文