.NET 表格布局面板

发布于 2024-10-21 00:28:54 字数 3379 浏览 4 评论 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();
    }

提前致谢。

Basically I have a tablelayoutpanel , it its currently being used for POS System.
When I call SetColumnSpan on a button control , the tablelayoutpanel adds an extra row, and messes up my screen layout.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余厌 2024-10-28 00:28:54

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

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

确保从计划跨越的单元格中删除所有控件。

此外,在某些情况下,表格布局会放弃,特别是当您使用自动调整大小跨单元格时。

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. 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.

森林很绿却致人迷途 2024-10-28 00:28:54

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

Are you setting the RowSpan to a value greater than the number of rows in the table? 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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文