1

I'm really new to C# coding. I got an array of buttons and I want to move the button I'm left clicking by mouse. Any advice?

private void CreateTable(Button[] Tabla)
     {
         int horizotal = 45;
         int vertical = 45;

         for (int i = 0; i < Tabla.Length; i++)
         {
             Tabla[i] = new Button();
             Tabla[i].BackColor = Color.Azure;
             Tabla[i].Size = new Size(45, 45);
             Tabla[i].Location = new Point(horizotal, vertical);
              if ((i == 14) || (i == 29) || (i == 44) || (i == 59) || (i == 74) ||
                   (i == 89) || (i == 104) || (i == 119) || i == 134 || i == 149 || i == 164 || i == 179 || i == 194 || i == 209)
              {
                   vertical = 45;
                   horizotal = horizotal + 45;
              }
              else
                 vertical = vertical + 45;
              this.Controls.Add(Tabla[i]);
          }
    }

This is the code that creates my buttons.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • 1
    I don't know what you mean by saying "move the button...by mouse". Do you mean you want the button to move when clicked, or you want to be able to drag the button, like you can drag a file around on your desktop? – Curtis Rutland Apr 25 '14 at 20:22
  • Basically I'm trying to make a scrabble game, and I want to drag a button ontop of another so they kinda merge. Sorry if I'm not clear, I know my english is pretty bad. – user3574409 Apr 25 '14 at 20:24
  • 2
    The functionality you are referring to is called "Drag and Drop". See this article as a starting point of how to do that on a Windows Form application: http://stackoverflow.com/questions/3728870/drag-and-drop-windows-forms-button – Kevin Apr 25 '14 at 20:45
  • @Kevin is correct, please read more about "Drag and Drop" – Only a Curious Mind Apr 25 '14 at 20:46

1 Answers1

1

First of all, you add mouseeventhandler your buttons then you declare the mouseeventshandler like this.

 private void CreateTable(Button[] Tabla)
 {
     int horizotal = 45;
     int vertical = 45;

     for (int i = 0; i < Tabla.Length; i++)
     {
         Tabla[i] = new Button();
         Tabla[i].BackColor = Color.Azure;
         Tabla[i].Size = new Size(45, 45);
         Tabla[i].Location = new Point(horizotal, vertical);
          if ((i == 14) || (i == 29) || (i == 44) || (i == 59) || (i == 74) ||
               (i == 89) || (i == 104) || (i == 119) || i == 134 || i == 149 || i == 164 || i == 179 || i == 194 || i == 209)
          {
               vertical = 45;
               horizotal = horizotal + 45;
          }
          else
             vertical = vertical + 45;
          Tabla[i].MouseDown += new MouseEventHandler(button_down);//adding Down handler
          Tabla[i].MouseMove += new MouseEventHandler(button_move);//adding Move handler
          this.Controls.Add(Tabla[i]);
      }
}
    private void button_down(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            starting.X = e.Location.X;
            starting.Y = e.Location.Y;
        }
    }

    private void button_move(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ((Button)sender).Left += e.Location.X - starting.X;
            ((Button)sender).Top += e.Location.Y - starting.Y;
        }
    }

sender is caller of the handler and we know it is a button. So we can use it like this. ı think this code is perfect for you but your problem is using buttons for letter. You can use picturebox for letter and theyare more proper than buttons. Finally I think you should declare field your buttons array.

Hakan SONMEZ
  • 2,176
  • 2
  • 21
  • 32