如果我不将按钮拖动到我想拖动的位置,它如何到达第一个位置?

How does it go to its first position if I don't drag the button where I want to drag it?

enter image description here我有 9 个按钮。三个位于上方,三个位于下方。所有 3 个按钮都在右侧。上面按钮上写的单词的反义词写在右边的按钮上。我想将带有这些反义词的按钮放在底部的按钮上。底部的按钮是彩色的,它们的文本与右侧的按钮相同,但它们的文本是不可见的,因为它与按钮颜色相同。所以底部的按钮看起来像一个彩色的盒子。这就是我现在想做的;例如,按钮 1 = 冷,按钮 7 = 热。我想将此按钮 7 放在按钮 1 下方的按钮 4 上,但如果我想将它放在按钮 5 上,我希望它 return 到其原始位置。在我写的代码里,一按button7就直接跳到button4,其他的按钮我试不下来。我如何尝试其他按钮并将它们 return 放在 Visual studio 表单应用程序中的第一个位置?

enter code here
   using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Drawing;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;
   using System.Windows.Forms;

   namespace materyalll
   {
       public partial class Form1 : Form
       {
           public Form1()
           {
               InitializeComponent();
               startlocation = new Point(button7.Left, button7.Top);
               startlocation2 = new Point(button8.Left, button8.Top);
               startlocation3 = new Point(button9.Left, button9.Top);

           }
           Point location, location2, location3, startlocation, startlocation2, startlocation3;


           private void button7_MouseDown(object sender, MouseEventArgs e)
           {
               location = e.Location;
           }

           private void button7_MouseMove(object sender, MouseEventArgs e)
           {
               if (e.Button == MouseButtons.Left)
               {
                   button7.Left += e.X - (location.X);
                   button7.Top += e.Y - (location.Y);
               }

           }

           private void button7_MouseUp(object sender, MouseEventArgs e)
           {
               if (button7.Text == button4.Text)
                   button7.Location = button4.Location;
               else if (button7.Text != button5.Text)
                   button7.Location = startlocation;
               else if (button7.Text != button6.Text)
                   button7.Location = startlocation;

           }

           private void button8_MouseDown(object sender, MouseEventArgs e)
           {
               location2 = e.Location;
           }

           private void button8_MouseMove(object sender, MouseEventArgs e)
           {
               if (e.Button == MouseButtons.Left)
               {
                   button8.Left += e.X - (location2.X);
                   button8.Top += e.Y - (location2.Y);
               }
           }       

           private void button8_MouseUp(object sender, MouseEventArgs e)
           {
               if (button8.Text == button5.Text)
                   button8.Location = button5.Location;
               else if (button8.Text != button4.Text)
                   button8.Location = startlocation2;
               else if (button8.Text != button6.Text)
                   button8.Location = startlocation2;
           }

           private void button9_MouseDown(object sender, MouseEventArgs e)
           {
               location3 = e.Location;
           }

           private void button9_MouseMove(object sender, MouseEventArgs e)
           {
               if (e.Button == MouseButtons.Left)
               {
                   button9.Left += e.X - (location3.X);
                   button9.Top += e.Y - (location3.Y);
               }
           }

           private void button9_MouseUp(object sender, MouseEventArgs e)
           {
        
               if (button9.Text == button6.Text)
                   button9.Location = button6.Location;
               else if (button9.Text != button4.Text)
                   button9.Location = startlocation3;
               else if (button9.Text != button5.Text)
                   button9.Location = startlocation3;

           }
       }
   }

尝试这样的事情:

Dictionary<string, Point> originalPoints = new Dictionary<string, Point>();
originalPoints.Add(nameof(button1), new Point(button1.Left, button1.Top));
// repeat this for all buttons

void SetToOriginalPosition(Button button)
{
    Point p = originalPoints[nameof(button)];
    button.Left = p.X;
    button.Top = p.Y;
}

将 Button 的原始位置存储在 Button 本身的 .Tag 属性 中。当用户释放鼠标时,查看正确目标按钮的矩形和当前按钮是否相交。如果他们不这样做,则快速回到标签中的存储位置;否则捕捉到正确按钮的位置。

这里是 button7:

public Form1()
{
   InitializeComponent();
   button7.Tag = button7.Location;
   button8.Tag = button8.Location;
   button9.Tag = button9.Location;
}

private void button7_MouseUp(object sender, MouseEventArgs e)
{
    if (button7.Bounds.IntersectsWith(button4.Bounds)) {
        button7.Location = button4.Location;
    }
    else
    {
        button7.Location = (Point)button7.Tag;
    }
}

其他两个按钮的代码非常相似。