Click here to Skip to main content
15,915,086 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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
Posted
Updated 26-May-14 23:32pm
v3
Comments
Bh@gyesh 27-May-14 5:44am    
Add btn.Autosize = true option .If it is not working then replace code of button2_MouseMove event with following code :

Button button = (Button)sender;
if (e.Button == MouseButtons.Left)
{
button .Left += e.X - move.X;
button .Top += e.Y - move.Y;
}
jackspero18 27-May-14 5:58am    
No it is also not working there is an error on move

set the Location property like below
C#
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
    Button button = sender as Button;
    button.Location = new Point(button1.Left - 1, button.Top);
}
 
Share this answer
 
Comments
jackspero18 27-May-14 5:11am    
no bro its not working
C#
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.

      }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900