0

基本上我有一个 tablelayoutpanel ,它目前用于 POS 系统。当我在按钮控件上调用 SetColumnSpan 时,tablelayoutpanel 添加了一个额外的行,并弄乱了我的屏幕布局。

有没有人遇到过这个?

面板中的每个空闲空间都分配了一个空白按钮,当屏幕处于编辑模式时,他们可以添加/编辑和删除按钮。下面是应用按钮更改的代码。

编辑清理代码

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)
        {

        }
    }

这是序列化按钮布局的按钮管理器函数。

    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);
    }

这是使用按钮加载/填充屏幕的代码

 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();
    }

提前致谢。

4

2 回答 2

1

您是否将 RowSpan 设置为大于表中行数的值?这可能会导致渲染额外的行。除此之外,您将需要提供更多信息/代码让我们弄清楚:)

于 2011-03-08T14:45:52.817 回答
1

确保跨单元格中没有控件。

如果在单元格 0,0 上将列跨度设置为 2,并将控件放在 1,0 中,则会混淆布局引擎。由于您在问题中指定向所有单元格添加空白按钮,这可能就是这里发生的情况。

确保从您计划跨越的单元格中删除任何控件。

此外,在某些情况下,表格布局会放弃,特别是如果您跨越具有自动调整大小的单元格。

于 2011-03-08T17:05:18.443 回答