基本上,我有一个表外面板,它目前是用于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();
}谢谢你的进阶。
发布于 2011-03-08 17:05:18
确保您在跨单元格中没有控制权。
如果将单元格0,0上的列span设置为2,并将控件设为1,0,则会混淆布局引擎。由于您在问题中指定向所有单元格中添加了空白按钮,因此这里可能会发生这种情况。
确保从计划跨越的单元格中移除任何控件。
此外,在某些情况下,表布局只是放弃,特别是如果您跨单元格自动调整大小。
发布于 2011-03-08 14:45:52
是否将RowSpan设置为大于表中行数的值?这可能会导致呈现额外的行。除此之外,您还需要提供更多的信息/代码,以便我们找出答案:)
https://stackoverflow.com/questions/5233953
复制相似问题