简体   繁体   English

如何通过鼠标将按钮从按钮阵列中移出? C#

[英]How to move a button from an array of buttons by mouse? C#

I'm really new to C# coding. 我真的是C#编码的新手。 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. 这是创建我的按钮的代码。

First of all, you add mouseeventhandler your buttons then you declare the mouseeventshandler like this. 首先,添加mouseeventhandler您的按钮,然后像这样声明mouseeventshandler。

 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. 最后,我认为您应该声明字段您的按钮数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM