将 .Net UserControl 添加到 TabPage 时出现大小调整问题
我有一个复杂的 Windows 窗体 GUI 程序,其中包含大量自动控件生成和操作。我需要做的一件事是将自定义用户控件添加到新实例化的 TabPage 中。但是,当我的代码执行此操作时,我会收到自动调整大小事件,导致格式变得丑陋。在不详细说明可能涉及的所有不同容器的情况下,基本问题是这样的:
在代码中的某个点,我创建了一个新的选项卡页:
TabPage tempTabPage = new TabPage("A New Tab Page");
然后我将其设置为我希望它保持的特定大小:
tempTabPage.Width = 1008;
tempTabPage.Height = 621;
然后我将其添加到 TabControl:
tabControl.TabPages.Add(tempTabPage);
然后我创建一个用户控件,我希望它出现在新添加的 TabPage 中:
CustomView customView = new CustomView("A new custom control");
这就是问题所在。此时 tempTabPage 和 customView 的大小相同,没有填充或边距,并且它们就是我想要的尺寸。我现在尝试将这个新的自定义 UserControl 添加到选项卡页,如下所示:
tempTabPage.Controls.Add(customView);
进行此调用时,customView 及其子控件的大小会调整为更大,因此 customView 的部分内容会被隐藏。
谁能给我任何指导,告诉我要寻找什么或可能导致此类问题的原因是什么?
提前致谢。
I have a complex Windows Forms GUI program that has a lot of automated control generation and manipulation. One thing that I need to be able to do is add a custom UserControl to a newly instatiated TabPage. However, when my code does this I get automatic resizing events that cause the formatting to get ugly. Without detailing all of the different Containers that could possibly be involved, the basic issue is this:
At a certain point in the code I create a new tab page:
TabPage tempTabPage = new TabPage("A New Tab Page");
Then I set it to a certain size that I want it to maintain:
tempTabPage.Width = 1008;
tempTabPage.Height = 621;
Then I add it to a TabControl:
tabControl.TabPages.Add(tempTabPage);
Then I create a user control that I want to appear in the newly added TabPage:
CustomView customView = new CustomView("A new custom control");
Here is where the problem comes in. At this point both the tempTabPage and the customView are the same size with no padding or margin and they are the size I want them to be. I now try to add this new custom UserControl to the tab page like this:
tempTabPage.Controls.Add(customView);
When making this call the customView and it's children controls get resized to be larger and so parts of the customView are hidden.
Can anyone give me any direction on what to look for or what could be causing this kind of issue?
Thanks ahead of time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UserControl 的“AutoScaleMode”属性应设置为“None”。
The UserControl's "AutoScaleMode" property should be set to "None".
如果您希望
customView
填充TabPage
。像这样使用
Dock
:然后
customView
将填充TabPage
中的空间,但您必须处理customView 的大小调整
这样子控件就会正确显示。If you want the
customView
to fill theTabPage
.Use
Dock
like this:Then the
customView
will fill out the space in theTabPage
, but you have to handle resizing of thecustomView
so child controls will be shown properly.我有同样的问题。
UserControl 的“AutoScaleMode”设置为“None”对我有用。
I had the same issue.
The UserControl's "AutoScaleMode" set to "None" works for me.