简体   繁体   English

.NET TableLayoutPanel

[英].NET TableLayoutPanel

Basically I have a tablelayoutpanel , it its currently being used for POS System. 基本上我有一个tablelayoutpanel,它目前用于POS系统。 When I call SetColumnSpan on a button control , the tablelayoutpanel adds an extra row, and messes up my screen layout. 当我在按钮控件上调用SetColumnSpan时,tablelayoutpanel会添加一个额外的行,并且会弄乱我的屏幕布局。

Has anybody come across this before ? 以前有人遇到过这个吗?

Each free space in the panel is assigned a blank button, when the screen is in edit mode , they can add/edit and delete buttons. 面板中的每个可用空间都分配了一个空白按钮,当屏幕处于编辑模式时,它们可以添加/编辑和删除按钮。 Below is the code to apply button changes. 以下是应用按钮更改的代码。

Edit cleaned up code a bit 编辑清理过的代码

void button_MouseUp(object sender, EventArgs e)
    {
        try
        {
            TableLayoutPanelCellPosition pos = tableLayoutPanel1.GetCellPosition((Control) sender);
            POSButton productButton = GetProductButton(sender);

            tableLayoutPanel1.SuspendLayout();

            if (productButton == null)
            {
                DeleteButton(sender, pos);
                return;
            }

            productButton.Dock = DockStyle.Fill;

            EditModeHookButton(productButton);
            tableLayoutPanel1.Controls.Remove((Control) sender);

            tableLayoutPanel1.Controls.Add(productButton, pos.Column, pos.Row);

            if (productButton.TableRowSpan > 0)
                tableLayoutPanel1.SetRowSpan(productButton, productButton.TableRowSpan);

            if (productButton.TableColumnSpan > 0)
                tableLayoutPanel1.SetColumnSpan(productButton, productButton.TableColumnSpan);

            buttonManager.Save(tableLayoutPanel1);
            tableLayoutPanel1.ResumeLayout();
        }
        catch(OperationCanceledException)
        {

        }
    }

Here is the button Manager function that serializes the button layout. 这是按钮管理器功能,它序列化按钮布局。

    public void Save(ScreenTabkeLayoutPanel panel)
    {
        List<ButtonSaveInfo> buttons = new List<ButtonSaveInfo>();
        foreach (Control control in panel.Controls)
        {
            TableLayoutPanelCellPosition pos = panel.GetCellPosition(control);
            ButtonSaveInfo info;

            if (control is POSButton)
                info = ((POSButton)control).ConvertToButtonInfo(pos);
            else
                info = control.ConvertToButtonInfo(pos);

            buttons.Add(info);
        }

        AppDataSerializer.SaveBinary(buttons,buttonPath);
    }

Here is the code that loads/populates the screen with the buttons 这是使用按钮加载/填充屏幕的代码

 private void LoadButtonsFromFile(ScreenTabkeLayoutPanel panel)
    {
        List<ButtonSaveInfo> buttons = AppDataSerializer.LoadBinary<List<ButtonSaveInfo>>(buttonPath);
        panel.SuspendLayout();
        foreach (ButtonSaveInfo info in buttons)
        {
            switch (info.ButtonType)
            {
                case (int) ButtonType.PRODUCT:

                    POSButton productButton = info.ConvertToPosButton();
                    wireButtonEvents(productButton);
                    panel.Controls.Add(productButton, info.ColumnIndex, info.RowIndex);

                    if (productButton.TableRowSpan > 0)
                        panel.SetRowSpan(productButton, productButton.TableRowSpan);

                    if (productButton.TableColumnSpan > 0)
                        panel.SetColumnSpan(productButton, productButton.TableColumnSpan);


                    break;

                default:
                    Control control = BuildBlankButton();
                    wireButtonEvents(control);
                    panel.Controls.Add(control, info.ColumnIndex, info.RowIndex);
                    break;
            }
        }
        FillEmptySpacesWillBlankButtons(panel);
        panel.ResumeLayout();
    }

Thanks in advanced. 提前致谢。

Are you setting the RowSpan to a value greater than the number of rows in the table? 您是否将RowSpan设置为大于表中行数的值? This might cause an extra row to be rendered. 这可能会导致额外的行被渲染。 Other than that you will need to provide more information/code for us to figure it out :) 除此之外,您需要提供更多信息/代码,以便我们弄明白:)

Make sure you don't have a control in a spanned cell. 确保在跨区单元格中没有控件。

If you set column span to 2 on cell 0,0 and put a control in 1,0 this will confuse the layout engine. 如果在单元格0,0上将列跨度设置为2并将控件放在1,0中,则会混淆布局引擎。 Since you specified in your question that you added blank buttons to all cells, this might be what is happening here. 由于您在问题中指定向所有单元格添加了空白按钮,因此可能会发生这种情况。

Make sure you remove any control from a cell you are planning to span over. 确保从计划跨越的单元格中删除任何控件。

Also, there are some situation in which the table layout just gives up, especially if you span cells with auto sizing. 此外,在某种情况下,表格布局只是放弃了,特别是如果您跨越具有自动调整大小的单元格。

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

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