简体   繁体   中英

.NET TableLayoutPanel

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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