WinForms 锚定控件无法正确最大化
我对 WinForms (VB.NET) 有疑问。
主窗口是一个 MDI 容器。 用户打开一个新的子窗口:
然后将其最大化,以便窗口正确填充客户区域。我的控件正确锚定(使用 IDE 属性 Anchor)到窗口两侧,以便放大窗口始终能很好地填充客户区域:
在这种状态下(客户端窗口最大化),用户打开一个不同的或新的子窗口,但新的窗口控件不会拉伸,也就是说,它们不“理解”它们应该拉伸!
这特别烦人,因为如果用户尝试恢复窗口,则控件会被拉伸,因此它们消失了(看到不再有列表视图)!
这是一个错误吗?我该如何解决这个问题?
编辑:根据 Hans Passant 的评论,我创建了一个新的简单项目,它按预期工作。因此我进行了调查,看看与我的真实项目和示例有何不同。不同之处在于,在我的项目中,我动态创建表单。
我在工具栏上动态创建许多按钮。当用户单击按钮时,这是执行的代码:
Private Sub buttonClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
Cursor.Current = Cursors.WaitCursor
Dim b As Button = CType(sender, Button)
Dim assem As Assembly = Assembly.GetExecutingAssembly()
Dim formType As Type = assem.GetType(CStr(b.Tag))
Dim exFormAsObj As Object = Nothing
Try
exFormAsObj = Activator.CreateInstance(formType)
Catch ex As Exception
Cursor.Current = Cursors.Default
MessageBox.Show("clicca meglio:" + ex.ToString)
Exit Sub
End Try
Dim f As Form = CType(exFormAsObj, Form)
f.MdiParent = Me
f.Show()
Cursor.Current = Cursors.Default
End Sub
也就是说,表单名称位于按钮标记中。我使用 Activator.CreateInstance(formType)
创建表单的新实例,然后显示它:f.Show()
。
我很确定问题出在这个动态子表单创建中,但我不知道在哪里。
编辑2: 成立!在我的表单常见 Load 事件中,我应该写
myform.SuspendLayout()
' various instructions
myform.ResumeLayout(False)
true 而不是 False 我应该写 true: myform.ResumeLayout(True)
很简单,抱歉。
I have an issue with WinForms (VB.NET).
The main window is an MDI container.
The user opens a new child window:
and then maximizes it, so the window correctly fills up the client area. My controls are properly anchored (with the IDE property Anchor) to the window sides so that enlarging the window always fills nicely the client area:
In this state (client windows maximized) the user opens a different or a new child window, but the new window controls are not stretched, that is they don't "understand" they should stretch!
This is particularly annoying because if the user tries to restore the window, then the controls are stretched in, so they disappear (see there's no more the listview)!
Is this a bug? How can I solve this?
edit: as per Hans Passant's comment, I created a new simple project and it worked as it should. So I investigated to see what was different from my real project and the sample. The difference is that in my project I create forms dynamically.
I dynamically create many buttons on a toolbar. When the user clicks a button, this is the code that gets executed:
Private Sub buttonClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
Cursor.Current = Cursors.WaitCursor
Dim b As Button = CType(sender, Button)
Dim assem As Assembly = Assembly.GetExecutingAssembly()
Dim formType As Type = assem.GetType(CStr(b.Tag))
Dim exFormAsObj As Object = Nothing
Try
exFormAsObj = Activator.CreateInstance(formType)
Catch ex As Exception
Cursor.Current = Cursors.Default
MessageBox.Show("clicca meglio:" + ex.ToString)
Exit Sub
End Try
Dim f As Form = CType(exFormAsObj, Form)
f.MdiParent = Me
f.Show()
Cursor.Current = Cursors.Default
End Sub
That is, the form name is in the button tag. I create a new instance of the form, with Activator.CreateInstance(formType)
then I show it: f.Show()
.
I am pretty sure that the problem is in this dynamic child form creation, but I can't get where.
edit2:
Found! In my form common Load event I was doing
myform.SuspendLayout()
' various instructions
myform.ResumeLayout(False)
instead of False I should have written true: myform.ResumeLayout(True)
So simple, I'm sorry.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了解决方案(感谢科迪·格雷建议在这里发布我自己的答案)。
在我的表单常见 Load 事件中,我正在执行:
而不是
False
我应该编写true
:myform.ResumeLayout(True)
简单,但很棘手。
谢谢大家。
I've found the solution, (thanks Cody Gray to suggesting to post my own answer here).
In my form common Load event I was doing:
instead of
False
I should have writtentrue
:myform.ResumeLayout(True)
Simple, but tricky.
Thanks all.
我认为您可能想要实现的目标是使用
或其亲戚之一来完成。
请记住,通常不鼓励使用 MDI 布局。
I think what you might want to achieve is done using
or one of its relatives.
Keep in mind that MDI layouts are in general discouraged.
也许尝试实例化表单并在加载事件中以编程方式设置属性。
如果一个实例有效,但其他实例无效,则意味着您需要使用调试器检查实例化表单的属性,以查看它们是否确实按照您期望的方式设置。
Perhaps try instancing the form and set the properties programmatically in the load event.
If one instance works, but other instances do not, that means you need to inspect the properties of the instanced forms with the debugger to see if they are indeed set the way you expect.
我在 mdi 子窗体上使用 datagridview,所有四个边都锚定,只要创建的窗体未最大化,它的大小就会正确。如果我在其他子项最大化时创建表单,则即使您手动规范化并调整表单大小,它的大小也会不正确并且永远不会自行纠正。我的解决方案是在表格上放置一个面板,所有四个边都固定,并将 DGV 放置在面板中,并将停靠设置为文件。我不知道为什么我必须这样做,但它确实解决了问题。
I was using a datagridview on an mdi child form with all four sides anchored and it would size properly as long as the form was created not maximized. If I created the form while other children were maximized, it sized incorrectly and never corrected itself even if you normalized and resized the form manually. My solution was to put a panel on the form with all four sides anchored and placing the DGV in the panel with dock set to file. I have no idea why I had to do this, but it DID fix the problem.