Sizing issues while adding a .Net UserControl to a

2019-02-22 03:59发布

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.

3条回答
一纸荒年 Trace。
2楼-- · 2019-02-22 04:20

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楼-- · 2019-02-22 04:32

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

查看更多
虎瘦雄心在
4楼-- · 2019-02-22 04:41

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

查看更多
登录 后发表回答