Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Center controls inside a container

$
0
0

I need to drag controls (Label) on panel and when dragging I want to move the control.  I did it this way:

private Point MouseDownLocation; 

        private void Form1_Load(object sender, EventArgs e) 
        { 

        } 

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

            } 
        } 

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

        private void panel1_DragOver(object sender, DragEventArgs e) 
        { 
            e.Effect = DragDropEffects.Move; 
        } 

I now need to have labels in the center of the container when I drop them. How to do it?


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>