本文介绍了如何在Windows窗体中移动动态创建的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i已经创建了桌面应用程序,我正在尝试移动动态生成的按钮

但是它不动了

但这个代码正在处理静态单键

代码如下:



Hello guys,
i have created desktop application, i am trying to move button which dynamically generated
but its not moving
But this code is working on static single button
code below:

public void btt()
       {
           ArrayList ar2 = new ArrayList();
           for (int x = 0; x <= 10; x++)
           {

               ar2.Add(x.ToString());
               Button btn = new Button();
               btn.Text = "" + ar2[x].ToString() + "";
               btn.Height = 44;
               btn.Width = 103;
               btn.Cursor = Cursors.Hand;
               btn.MouseDown += new MouseEventHandler(button2_MouseDown);
               btn.MouseDown += new MouseEventHandler(button2_MouseMove);

               this.Controls.Add(btn);
           }
       }


private Point MouseDownLocation;
       private void button2_MouseDown(object sender, MouseEventArgs e)
       {
           if (e.Button == System.Windows.Forms.MouseButtons.Left)
           {
               MouseDownLocation = e.Location;

           }
       }

       private void button2_MouseMove(object sender, MouseEventArgs e)
       {
           if (e.Button == System.Windows.Forms.MouseButtons.Left)
           {
               Button button = sender as Button;
               button.Left = e.X + button.Left - MouseDownLocation.X;
               button.Top = e.Y + button.Top - MouseDownLocation.Y;


           }
       }





先谢谢



Thanks in advance

推荐答案

if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
    Button button = sender as Button;
    button.Location = new Point(button1.Left - 1, button.Top);
}


private void button3_Click(object sender, EventArgs e)
       {
          ArrayList ar2 = new ArrayList();
          for (int x = 0; x <= 10; x++)
          {

              ar2.Add(x.ToString());
              Button qwe = new Button();
              qwe.Text = "" + ar2[x].ToString() + "";
              qwe.Height = 44;
              qwe.Width = 103;
              qwe.Cursor = Cursors.Hand;
              qwe.AutoSize = true;
              qwe.Size = new Size(40, 40);

              qwe.Name = "b" + x.ToString();

          qwe.Left = 4 + (50 * x);

              qwe.MouseDown += new MouseEventHandler(qwe_MouseDown);

              qwe.MouseMove += new MouseEventHandler(qwe_MouseMove);

              qwe.MouseUp += new MouseEventHandler(qwe_MouseUp);
              flowLayoutPanel1.Controls.Add(qwe);
          }
       }
        bool suruklenmedurumu2 = false;

      Point ilkkonum2;

      void qwe_MouseDown(object sender, MouseEventArgs e)

      {

         suruklenmedurumu2 = true; //işlemi burada true diyerek başlatıyoruz.

          ilkkonum2 = e.Location; //İlk konuma gördüğünüz gibi değerimizi atıyoruz.

      }
       void qwe_MouseMove(object sender,MouseEventArgs e)

      {


         if (suruklenmedurumu2 == true)

      {
          Button qwe = sender as Button;
          qwe.Left = e.X + qwe.Left - (ilkkonum2.X);

          qwe.Top = e.Y + qwe.Top - (ilkkonum2.Y);
          label1.Text = qwe.Location.X.ToString();
          label2.Text = qwe.Location.Y.ToString();

       }

      }

      void qwe_MouseUp(object sender, MouseEventArgs e)

      {

          suruklenmedurumu2 = false; //Sol tuştan elimizi çektik artık yani sürükle işlemi bitti.

          this.Cursor = Cursors.Default; //İmlecimiz(Cursor) default değerini alıyor.

      }


这篇关于如何在Windows窗体中移动动态创建的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 20:41