Search code examples
c#mousedownmouseup

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


enter image description hereI have 9 buttons. Three are located above, 3 are located below them. All 3 buttons are on the right side. The antonyms of the words written on the buttons above are written on the buttons on the right. I would like to place the buttons with these antonyms on the buttons at the bottom. The buttons at the bottom are colored and their text is the same as the buttons on the right, but their text is not visible because it is the same as the button color. So the buttons on the bottom look like a colored box. That's what I want to do now; for example, button 1 = cold and button7 = hot. I want to place this button 7 on button 4 located below button 1, but if I want to place it on button 5 I want it to return to its original position. In the code I wrote, as soon as I press button7, it goes directly to button4, I can't try the other buttons. how can I try other buttons and return them to the first position in Visual studio form application?

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;

           }
       }
   }

Solution

  • Store the original location of the Button in the .Tag property of the Button itself. When the user releases the mouse, see if the RECTANGLES of the correct target button and the current button INTERSECT. If they don't, snap back to the stored location in the Tag; otherwise snap to the location of the correct button.

    Here it is with 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;
        }
    }
    

    The code for the other two buttons would be very similar.