あなたが望むものの鍵は、 のさまざまな位置を処理できる明確で拡張可能なアルゴリズムを設定することですPanels
。ここに、この問題に対する特定のアプローチを示す簡単なコードがあります。
public partial class Form1 : Form
{
int[] panelLocations;
Point[] pointLocations;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
panelLocations = new int[5];
pointLocations = new Point[5];
panelLocations[1] = 1;
panelLocations[2] = 2;
panelLocations[3] = 3;
pointLocations[1] = new Point(panel1.Left, panel1.Top);
pointLocations[2] = new Point(panel2.Left, panel2.Top);
pointLocations[3] = new Point(panel3.Left, panel3.Top);
}
private void relocate(int curPanel, bool goTop)
{
int curLoc = panelLocations[curPanel];
int newLoc = curLoc - 1;
if (!goTop)
{
newLoc = curLoc + 1;
}
if (newLoc < 1) newLoc = 3;
if (newLoc > 3) newLoc = 1;
if (newLoc != curLoc)
{
int otherIndex = Array.IndexOf(panelLocations, newLoc);
panelLocations[curPanel] = newLoc;
relocatePanel(curPanel);
panelLocations[otherIndex] = curLoc;
relocatePanel(otherIndex);
}
}
private void relocatePanel(int curIndex)
{
if (curIndex == 1)
{
panel1.Location = pointLocations[panelLocations[1]];
}
else if (curIndex == 2)
{
panel2.Location = pointLocations[panelLocations[2]];
}
else if (curIndex == 3)
{
panel3.Location = pointLocations[panelLocations[3]];
}
}
private void buttonTop1_Click(object sender, EventArgs e)
{
relocate(1, true);
}
private void buttonBottom1_Click(object sender, EventArgs e)
{
relocate(1, false);
}
}
新しいプロジェクトを開き、3 つのパネル ( Panel1
、Panel2
およびPanel3
... 別の背景色を配置したほうがよい) を追加し、2 つのボタン (buttonUp
およびbuttonDown
) を含めます。このコードは、Panel1
を上下させます (他のパネルとの位置を変更することによって)。
アイデアは非常に単純です。最初に、すべてのパネルの位置を配列に格納します。別の配列では、各パネルが毎回配置される場所を格納します (1 は の元の位置Panel1
など)。
これは非常に単純なコードであり、必要に応じて改善および拡張できますが、アイデアは非常に信頼性が高く、どのような場合にも使用できます。つまり、パネルが移動する固定位置のセットです。