简体   繁体   English

在C#中以编程方式创建TableLayoutPanel

[英]Create TableLayoutPanel programmatically in c#

I have TabControl that contains one TabPage , and other TabPages are added dynamically, on event. 我有包含一个TabPage TabControl ,其他TabPage是在事件发生时动态添加的。

First TabPage contains TableLayoutPanel , so I am wondering is there any chance to add new TableLayoutPanel to every new created TabPage , which is same as first TableLayoutPanel but empty? 第一个TabPage包含TableLayoutPanel ,所以我想知道是否有机会向每个新创建的TabPage添加新的TableLayoutPanel ,这与第一个TableLayoutPanel相同,但为空?

Or, in other words, could I create TableLayoutPanel programmatically? 或者换句话说,我可以通过编程方式创建TableLayoutPanel吗?

You can create any control programmatically, including TableLayoutPanel . 您可以通过编程方式创建任何控件,包括TableLayoutPanel Actually when you are using designer controls are also created programmatically - code is generated by Visual Studio. 实际上,当您使用设计器控件时,也会以编程方式创建-代码是由Visual Studio生成的。

Simply create new TableLayoutPanel control and set all it's properties (rows, columns, etc). 只需创建新的TableLayoutPanel控件并设置其所有属性(行,列等)即可。 Then add this control to Controls property of new tab page: 然后将此控件添加到新选项卡页面的“ Controls属性中:

TabPage page = new TabPage("Another page");
page.Controls.Add(new TableLayoutPanel { Dock = DockStyle.Fill});
tabControl1.TabPages.Add(page);

Another option for you is creating user control with already configured TableLayoutPanel . 您的另一个选择是使用已配置的TableLayoutPanel创建用户控件 In this case you will need only to create new user control and add it to tab page. 在这种情况下,您只需要创建新的用户控件并将其添加到选项卡页面即可。

Also you can create custom TableLayoutPanel with specified rows, columns and other settings. 您还可以使用指定的行,列和其他设置创建自定义TableLayoutPanel Eg this control will have two rows with predefined height of 25% and 75% 例如,此控件将具有两行,其预定义高度分别为25%和75%

public class CustomTableLayoutPanel : TableLayoutPanel
{
    public CustomTableLayoutPanel()
    {
        RowStyles.Clear();
        RowStyles.Add(new RowStyle(SizeType.Percent, 0.25F));
        RowStyles.Add(new RowStyle(SizeType.Percent, 0.75F));
        BackColor = Color.Beige;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM