Can not move my buttons in forms

Artjom Schmitke 26 Reputation points
2021-08-15T23:55:51.45+00:00

i am learning C# and i got to microsoft forms.
so in my book it tells me to create a "TabControl" and add 3 simple buttons in the first tab of the TabControl.
everytime i add a button, i can not reposition it anymore within the tabcontrol.
if i just want to move the button with the mouse, it always snaps back to its first position where i added it.
what exactly do i have to do, so i can move the buttons?

i added a picture below so u know what i mean.
there is nothing special, but the button1 in the picture, i can not move it at all, it always snaps back to this position.
i restarted visual studio and created a new project, nothing helped so far.

123414-bild.jpg

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,923 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,551 Reputation points
    2021-08-16T08:45:11.157+00:00

    The only time I've seen this is with Visual Studio 2019 where in short the form designer sometimes gets stuck. When this happens try single clicking on the button followed by moving the button with the arrow keys on your keyboard just a small distance. Next, save the form, add another button and see if you can now move the first button using the mouse.

    In regards to sometimes gets stuck, the designer is considered to still be in preview mode. If this were Visual Studio 2017 or earlier this issue as far as I'm aware of does not exists.


1 additional answer

Sort by: Most helpful
  1. Jack J Jun 24,801 Reputation points Microsoft Vendor
    2021-08-16T08:46:26.507+00:00

    @Artjom Schmitke , I want to know if this happened in the designer or at runtime. If you want to move the control at runtime, you could try to use Mouse_Move event and Mouse_Down event to move the buttons in the TabPage.

    Code:

     private Point MouseDownLocation;  
            private void Form1_Load(object sender, EventArgs e)  
            {  
                Button btn1 = new Button();  
                btn1.Text = "test";  
                btn1.Location = new Point(0, 0);  
                btn1.MouseDown += Btn1_MouseDown;  
                btn1.MouseMove += Btn1_MouseMove;  
                tabPage1.Controls.Add(btn1);  
                  
            }  
      
            private void Btn1_MouseMove(object sender, MouseEventArgs e)  
            {  
                Button btn = sender as Button;  
                if (e.Button == System.Windows.Forms.MouseButtons.Left)  
                {  
                    btn.Left = e.X + btn.Left - MouseDownLocation.X;  
                    btn.Top = e.Y + btn.Top - MouseDownLocation.Y;  
                }  
            }  
      
            private void Btn1_MouseDown(object sender, MouseEventArgs e)  
            {  
                if (e.Button == System.Windows.Forms.MouseButtons.Left)  
                {  
                    MouseDownLocation = e.Location;  
                }  
            }  
    

    Result:

    123499-newresult.gif


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.