Sizing issues while adding a .Net UserControl to a TabPage

放肆的年华 提交于 2019-12-30 18:42:37

问题


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.


回答1:


The UserControl's "AutoScaleMode" property should be set to "None".




回答2:


If you want the customView to fill the TabPage.

Use Dock like this:

tempTabPage.Controls.Add(customView);
customView.Dock = DockStyle.Fill;

Then the customView will fill out the space in the TabPage, but you have to handle resizing of the customView so child controls will be shown properly.




回答3:


I had the same issue. The UserControl's "AutoScaleMode" set to "None" works for me.



来源:https://stackoverflow.com/questions/2733521/sizing-issues-while-adding-a-net-usercontrol-to-a-tabpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!