2011-03-08 48 views
0

基本上我有一個tablelayoutpanel,它目前用於POS系統。 當我在按鈕控件上調用SetColumnSpan時,tablelayoutpanel會添加一行額外的行,並調整我的屏幕布局。.NET 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) 
     { 

     } 
    } 

這裏是Button Manager的功能序列化的按鍵佈局。

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

在先進的感謝。

回答

1

確保您沒有跨網格單元中的控件。

如果在單元格0,0上將列寬設置爲2,並將控件設置爲1,0,則會混淆佈局引擎。既然你在你的問題中指定了爲所有單元格添加空白按鈕,這可能是這裏發生的事情。

確保從計劃覆蓋的單元中移除任何控件。

此外,在某些情況下,表格佈局會放棄,特別是如果您使用自動調整大小跨度單元格時。

+0

Coincoin謝謝你,你在哪裏現貨! – Jamie 2011-03-08 18:47:03

1

您是否將RowSpan設置爲大於表中行數的值?這可能會導致一個額外的行被渲染。除此之外,您將需要提供更多的信息/代碼,以便我們弄清楚它:)

+0

Hi Kaius謝謝你的回答,沒有足夠的行和列,我會添加更多的代碼 – Jamie 2011-03-08 14:49:12