如何在创建自定义控件时覆盖表格布局面板的默认2x2。 [英] How to override the default 2x2 for a table layout panel when creating a custom control.

查看:78
本文介绍了如何在创建自定义控件时覆盖表格布局面板的默认2x2。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在创建基于桌面布局面板的自定义控件时遇到问题。



我可以覆盖InitLayout()中的默认2x2,但这不会反映在设计器视图中。有没有人有这方面的经验?或者任何人都可以建议正确的方法来解决这个问题吗?



谢谢

解决方案

添加新组件到你的项目,打开它的代码窗口,并修改代码,如下例所示,它使用System.Windows.Forms在600x400像素TableLayoutPanel:

中创建一个8x8布局; 

namespace CustomTblLayoutPnl
{
public partial class TableLayoutPanelEx:TableLayoutPanel
{
protected override void OnLayout(LayoutEventArgs levent)
{
//改善渲染
this.DoubleBuffered = true;

this.Width = 600;
this.Height = 400;
this.RowCount = 8;
this.ColumnCount = 8;

int RowHeight = this.Height / this.RowCount;
int ColWidth = this.Width / this.ColumnCount;

for(int i = 0; i< this.RowCount; i ++)
{
RowStyles.Add(new RowStyle(SizeType.Absolute,RowHeight));
}

for(int j = 0; j< this.ColumnCount; j ++)
{
ColumnStyles.Add(new ColumnStyle(SizeType.Absolute,ColWidth) ));
}

base.OnLayout(levent);
}

public TableLayoutPanelEx()
{
InitializeComponent();
}
}
}


InitLayout不会给你你想要的东西,因为它太早执行了。 br />


使用TableLayoutPanel,你必须在OnLayout中进行,比如Bill所示。



你不要必须把宽度和高度放在那里。您可以动态地执行此操作,如下所示:

 public partial class CustomTableLayoutPanel:TableLayoutPanel 
{
protected override void OnLayout(LayoutEventArgs levent)
{
this.ColumnCount = 5;
this.RowCount = 5;

单个colWidth = 100 / this.ColumnCount;
单行高度= 100 / this.RowCount;

foreach(this.ColumnStyles中的ColumnStyle colStyle)
{
colStyle.SizeType = SizeType.Percent;
colStyle.Width = colWidth;
}

foreach(此.RowStyles中的RowStyle rowStyle)
{
rowStyle.SizeType = SizeType.Percent;
rowStyle.Height = rowHeight;
}

base.OnLayout(levent);
}
}





TLP并非真正按照您的需要进行定制。这样做是有问题的。您无法更改网格中的行数和列数!



您可以修改上面的代码,为您提供行数和列数,甚至可以设置行和列列样式。但是,他们将永远锁定这些价值观。



为了获得各种其他尺寸的网格,你必须为你想要的每个尺寸制作一个自定义TLP !


Hi all,

Im currently having issues when creating a custom control based on the table layout panel.

I can override the default 2x2 in the InitLayout() but this doesn't reflect in the designer view. Has anyone had any experience with this? Or can anyone suggest the correct way to go about this?

Thanks

解决方案

Add a new Component to your Project, open its code window, and modify the code, as shown in the following example, which creates an 8x8 layout in a 600x400 pixel TableLayoutPanel:

using System.Windows.Forms;

namespace CustomTblLayoutPnl
{
    public partial class TableLayoutPanelEx : TableLayoutPanel
    {
        protected override void OnLayout(LayoutEventArgs levent)
        {
            // improve rendering
            this.DoubleBuffered = true;

            this.Width = 600;
            this.Height = 400;
            this.RowCount = 8;
            this.ColumnCount = 8;

            int RowHeight = this.Height / this.RowCount;
            int ColWidth = this.Width / this.ColumnCount;

            for (int i = 0; i < this.RowCount; i++)
            {
                RowStyles.Add(new RowStyle(SizeType.Absolute, RowHeight));
            }

            for (int j = 0; j < this.ColumnCount; j++)
            {
                ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, ColWidth));
            }

            base.OnLayout(levent);
        }

        public TableLayoutPanelEx()
        {
            InitializeComponent();
        }
    }
}


InitLayout won't give you what you want because it gets executed too early.

With the TableLayoutPanel, you have to do it in OnLayout, as Bill showed.

You don't have to put the Width and Height in there. You can do that dynamically, like this:

public partial class CustomTableLayoutPanel : TableLayoutPanel
    {
        protected override void OnLayout(LayoutEventArgs levent)
        {
            this.ColumnCount = 5; 
            this.RowCount = 5;

            Single colWidth = 100 / this.ColumnCount;
            Single rowHeight = 100 / this.RowCount;

            foreach (ColumnStyle colStyle in this.ColumnStyles)
            {
                colStyle.SizeType = SizeType.Percent;
                colStyle.Width = colWidth;
            }

            foreach (RowStyle rowStyle in this.RowStyles)
            {
                rowStyle.SizeType = SizeType.Percent;
                rowStyle.Height = rowHeight;
            }

            base.OnLayout(levent);
        }
    }



The TLP wasn't really designed to be customized like you want. There is a problem with doing it this way. You cannot change the number of rows and columns in the grid!

You can modify the code above to give you the number of rows and columns and even set the row and column styles. But, They will be forever locked to those values.

In order to get various other size grids, you'd have to make a custom TLP for every size you want!


这篇关于如何在创建自定义控件时覆盖表格布局面板的默认2x2。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆