Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all my friends.

Can any body help me to find how to move a control in my form?
For example: move label in other location in form. Further, how to do it for random place in form.

Thanks mehdi
Posted
Updated 2-Jan-11 22:46pm
v2
Comments
T.Saravanann 3-Jan-11 4:49am    
Hi your question is not clear.Move means what you mention,user drag and drop the control?

If you need to move your controls at run-time, you can change their Location[^] property.

Also check out this[^] excellent article which demonstrates how can you move your controls at run-time using the mouse.

:)
 
Share this answer
 
Comments
Manfred Rudolf Bihy 3-Jan-11 5:07am    
Good call! 5+
Hope this code may hep you.
private Boolean dragInProgress = false;
        private Control currentCtrl;

        private int MouseDownX = 0;
        private int MouseDownY = 0;

        public MyFormSetting()
        {
            InitializeComponent();
            MouseMoveEventRegister(new Control[] {labelPayeeName, labelDate, labelAmountInWords, labelAmount });
        }

        private void MouseMoveEventRegister(Control[] ctrlList)
        {
            foreach (Control ctrl in ctrlList)
            {
                ctrl.MouseDown += new System.Windows.Forms.MouseEventHandler(this.labelMouseDownHandler);
                ctrl.MouseMove += new System.Windows.Forms.MouseEventHandler(this.labelMouseMoveHandler);
                ctrl.MouseUp += new System.Windows.Forms.MouseEventHandler(this.labelMouseUpHandler);
            }
        }

        private void labelMouseDownHandler(object sender, MouseEventArgs e)
        {
            if (!dragInProgress)
            {
                dragInProgress = true;
                MouseDownX = e.X;
                MouseDownY = e.Y;

                currentCtrl = (Control)sender;
            }
        }

        private void labelMouseUpHandler(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.dragInProgress = false;
                currentCtrl = null;
            }
        }

        private void labelMouseMoveHandler(object sender, MouseEventArgs e)
        {
            if (dragInProgress)
            {
                Point temp = new Point();

                temp.X = this.currentCtrl.Location.X + (e.X - MouseDownX);
                temp.Y = this.currentCtrl.Location.Y + (e.Y - MouseDownY);

                this.currentCtrl.Location = temp;
            }
        }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Jan-11 17:07pm    
Too good (my 5) for a question that makes so little sense and is being repeated over and over with little variations.
Kasson 16-Jan-11 23:23pm    
Thanks Sakryukov.
Take a look at:
System.Windows.Forms.Control[^]

Setting the Left and Top properties should do the trick.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Feb-11 21:14pm    
As simple as that - a 5.
--SA

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