根据您的图片,您在面板中的每一行都有一个控件。因此,我建议您使用TableLayoutPanel
而不是FlowLayoutPanel
. 另外我会为面板中的项目创建用户控件。例如,它将有名称PriorityUserControl
和四个按钮来增加、减少、最大化、最小化它的“优先级”(我水平放置按钮只是为了在屏幕上保存位置):
接下来,在此用户控件中创建四个事件:
public event EventHandler PriorityMaximized;
public event EventHandler PriorityIncreased;
public event EventHandler PriorityDecreased;
public event EventHandler PriorityMinimized;
并在单击按钮时引发适当的事件:
private void topButton_Click(object sender, EventArgs e)
{
if (PriorityMaximized != null)
PriorityMaximized(this, EventArgs.Empty);
}
而已。我们有用户控制,告诉它是否要向上或向下移动。现在将用户控件添加到 TableLayoutPanel(手动或动态)并将这四个事件的相同事件处理程序订阅到所有用户控件。就像是:
// create user control and attach event handlers
PriorityUserControl control = new PriorityUserControl();
control.PriorityMaximized += priorityUserControl_PriorityMaximized;
control.PriorityMinimized += priorityUserControl_PriorityMinimized;
control.PriorityIncreased += priorityUserControl_PriorityIncreased;
control.PriorityDecreased += priorityUserControl_PriorityDecreased;
// add another row to table
panel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
panel.RowCount = panel.RowStyles.Count;
// add control table layout panel
panel.Controls.Add(control);
panel.SetRow(control, panel.RowCount - 1);
好的。您现在应该做的就是实现这些事件处理程序。这很简单。例如降低优先级(即向下移动):
private void priorityUserControl_PriorityDecreased(object sender, EventArgs e)
{
// sender is a control where you clicked Down button
Control currentControl = (Control)sender;
// get position in panel
var position = panel.GetPositionFromControl(currentControl);
// just to be sure control is not one at the bottom
if (position.Row == panel.RowCount - 1)
return;
// we want to switch with control beneath current
Control controlToSwitch = panel.GetControlFromPosition(0, position.Row + 1);
// move both controls
panel.SetRow(currentControl, position.Row + 1);
panel.SetRow(controlToSwitch, position.Row);
}
现在实现最大化优先级(即移到顶部):
private void priorityUserControl_PriorityMaximized(object sender, EventArgs e)
{
Control currentControl = (Control)sender;
var position = panel.GetPositionFromControl(currentControl);
if (position.Row == 0 || panel.RowCount < 2)
return;
Control topControl = panel.GetControlFromPosition(0, 0);
panel.SetRow(currentControl, 0);
panel.SetRow(topControl, position.Row);
}
我相信你会自己创建休息两个处理程序。