Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Dear Friends,

I am developing one small project, in my project i have one panel, inside of this panel have some controls like picture box, text boxes etc.... I want to move this controls only inside of this panel when the mouse move. I finished to move controls but it is moving outside of my panel also.

I want to move controls only inside of panel when i move mouse.

Please reply [Removed Urgency]

[edit]SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 29-Apr-20 7:14am
v3
Comments
OriginalGriff 25-Jun-12 5:23am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
willempipi 25-Jun-12 5:35am    
Can you supply us with some code and information about how you move the controls?

To resolve this issue, please consider the panel left and top values for your piece of code,

May be the below solution is use full for you!

C#
if(panel1.Left < e.X)
            Control.Left = e.X;
            if(panel1.Top <e.Y)
            Control.Top = e.Y;
 
Share this answer
 
Comments
vasanthkumarmk 25-Jun-12 7:36am    
Here below i shown the code what i am using, But this one is suitable for only Left and Top side (Controls not moves outside of panel), This type not support for Right and Bottom Side, Both sides controls moves outside of panel. Please give me the coding for Right and Bottom position controls not move outside of panel.

if (e.Button == MouseButtons.Left)
{
if (pictureBox1.Left + (e.X - x) > 0)
pictureBox1.Left += (e.X - x);
if (pictureBox1.Top + (e.Y - y) > 0)
pictureBox1.Top += (e.Y - y);
}
We can't set right and bottom property values! These values are readonly so we always need to modify Top and Left properties only.

Hope the below approach is useful for you!

Take 2 panels and place them in bottom and right side of your panel.
set topBoundary and rightBoundary as per your requirement. I tried this approach is working fine.

C#
int topBoundary;
        int rightBoundary;
        private void Form1_Load(object sender, EventArgs e)
        {
            topBoundary = panel1.Bottom - 60;
            rightBoundary = panel1.Right - 100;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            checkBox1.Top = e.Y;
            checkBox1.Left = e.X;
        }

        private void panel2_MouseEnter(object sender, EventArgs e)
        {
            checkBox1.Top = topBoundary;
        }

        private void panel3_MouseEnter(object sender, EventArgs e)
        {
            checkBox1.Left = rightBoundary;
        }
 
Share this answer
 
Comments
vasanthkumarmk 25-Jun-12 9:02am    
Very Very Thanks for your code.....Its working but i confusing set the boundaries, Please tell me once, How can i set the boundaries.
Sandeep Mewara 26-Jun-12 6:16am    
Comment from Op:
Dear Dinesh,
I am using windows Application using C#, i have one doubt let i explain,

Explain :
I have one panel and one button control in windows application, When i click the Button I added the image of run time (Without using picture box control) of the panel, I need to select that image runtime when i click the mouse of that image, and that same time it able to re size (like Transform) that image eight right,left,top,bottom, side-left, side-right.

Please give me the Ideas how i do that. I try that last 20 days, still i didn't achieve.
Get your panel botton and panel right values in the run time and then adjust the boundaries.

Hope you got me!

Happy coding:)
 
Share this answer
 
Comments
vasanthkumarmk 26-Jun-12 2:06am    
ok Thank you..........
With this code you can add panels and move them ONLY in THE Panel they are deployed.



//Limitation Code////::START::::
// Mouse Button Left Down
                 if (e.Button == System.Windows.Forms.MouseButtons.Left) // The panel is attached to the mouse and you can move it
                 {


                             //Selected Panel Limited location of Bottom and Right
                             if (this.activeControl.Location.X == Math.Min(Math.Max(activeControl.Right + (e.X - previousPosition.X), 0), activeControl.Parent.Width - activeControl.Width) && this.activeControl.Location.Y == Math.Min(Math.Max(activeControl.Bottom + (e.Y - previousPosition.Y), 0), activeControl.Parent.Height - activeControl.Height))
                             {

                                 int RightX = Math.Min(Math.Max(activeControl.Right + (e.X - previousPosition.X), 0), activeControl.Parent.Width - activeControl.Width);
                                 int BottomY = Math.Min(Math.Max(activeControl.Bottom + (e.Y - previousPosition.Y), 0), activeControl.Parent.Height - activeControl.Height);

                                 activeControl.Location = new Point(RightX, BottomY);

                                 ///!LocationRightPanel.Equals(this.activeControl) && this.activeControl.Bounds.IntersectsWith(LocationRightPanel.Bounds)

                             }


                             //Selected Panel Limited location of LEFT and TOP
                             else
                             {
                                 int LeftX = Math.Min(Math.Max(activeControl.Left + (e.X - previousPosition.X), 0), activeControl.Parent.Width - activeControl.Width);
                                 int TopY = Math.Min(Math.Max(activeControl.Top + (e.Y - previousPosition.Y), 0), activeControl.Parent.Height - activeControl.Height);

                                 activeControl.Location = new Point(LeftX, TopY);

                             }

                 }













//////////-----FULL CODE::START:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace TaskbarTest
{
    public partial class Form1 : Form
    {

        private Control activeControl;
        private Point previousPosition;
        Panel TaskPanel;

       
        public Form1()
        {
            InitializeComponent();
        }







        // ADD Task - Button
        private void add_Task_Button_Click(object sender, EventArgs e)
        {
            AddTask();
        }


        


          // ADD task Method
        private void AddTask()
        {
            TaskPanel = new Panel();//Added task panel
            TaskPanel.Location = new System.Drawing.Point(30, 50);//Added panel Location
            TaskPanel.MaximumSize = new Size(300, 100); 
            TaskPanel.Size = new Size(150, 400); 
            TaskPanel.BackColor = Color.DarkBlue; 
            TaskPanel.Margin = new Padding(200, 200, 200, 200);
            TaskPanel.Padding = new Padding(200);
            TaskPanel.Name = "TaskPanel";
          

            // Event Handlers
            TaskPanel.MouseDown += new MouseEventHandler(TaskPanel_MouseDown);///Added panel Declaring MouseDown
            TaskPanel.MouseMove += new MouseEventHandler(TaskPanel_MouseMove);///Added panel Declaring MouseMove
            TaskPanel.MouseUp += new MouseEventHandler(TaskPanel_MouseUp);///Added panel Declaring MouseUp
            TaskPanel.LocationChanged += new System.EventHandler(TaskPanel_LocationChanged);
            TaskPanel.KeyDown += MovingPanel_KeyDown;
            TaskPanel.MouseClick += new MouseEventHandler(TaskPanel_MouseClick);

            loader_panel.Controls.Add(TaskPanel);// Add the panel to the loader_panel "Workplace"

            activeControl = TaskPanel;//Focused
        }



           // Mouse Down - "On holding the mouse button"
           private void TaskPanel_MouseDown(object sender, MouseEventArgs e)
           {

            activeControl = sender as Control;
            previousPosition = e.Location;// Start position on panel move
            Cursor = Cursors.Hand;

           }




          // Mouse Move
          private void TaskPanel_MouseMove(object sender, MouseEventArgs e)
          {

                    // If it is not sender does not move any panel  "The sender is the panel that owns the current MouseEvent"  
                    if (activeControl == null || activeControl != sender) 
                    {
                        return;//Terminates execution of the method and returns control to the calling method
                    }



                   // Mouse Button Left Down
                   if (e.Button == System.Windows.Forms.MouseButtons.Left) // The panel is attached to the mouse and you can move it 
                   {
                   
                   
                               //Selected Panel Limited location of Bottom and Right 
                               if (this.activeControl.Location.X == Math.Min(Math.Max(activeControl.Right + (e.X - previousPosition.X), 0), activeControl.Parent.Width - activeControl.Width) && this.activeControl.Location.Y == Math.Min(Math.Max(activeControl.Bottom + (e.Y - previousPosition.Y), 0), activeControl.Parent.Height - activeControl.Height)) 
                               {
                              
                                   int RightX = Math.Min(Math.Max(activeControl.Right + (e.X - previousPosition.X), 0), activeControl.Parent.Width - activeControl.Width);
                                   int BottomY = Math.Min(Math.Max(activeControl.Bottom + (e.Y - previousPosition.Y), 0), activeControl.Parent.Height - activeControl.Height);
                              
                                   activeControl.Location = new Point(RightX, BottomY);
                              
                                   ///!LocationRightPanel.Equals(this.activeControl) && this.activeControl.Bounds.IntersectsWith(LocationRightPanel.Bounds)
                              
                               }
                              
                              
                               //Selected Panel Limited location of LEFT and TOP 
                               else
                               {
                                   int LeftX = Math.Min(Math.Max(activeControl.Left + (e.X - previousPosition.X), 0), activeControl.Parent.Width - activeControl.Width);
                                   int TopY = Math.Min(Math.Max(activeControl.Top + (e.Y - previousPosition.Y), 0), activeControl.Parent.Height - activeControl.Height);
                              
                                   activeControl.Location = new Point(LeftX, TopY);
                              
                               }
                   
                   }
 
          }    
        
        

          private void TaskPanel_MouseUp(object sender, MouseEventArgs e)
          {
            activeControl = null;//Disable Control "You can now mouseclick on one dynamic panel to foucus on "Select"
            Cursor = Cursors.Default;
 
          }


          
          private void TaskPanel_LocationChanged(object sender, EventArgs e)
          {

          } 
        
        
           private void MovingPanel_KeyDown(object sender, KeyEventArgs e)
           {

           }
               
        
           private void TaskPanel_MouseClick(object sender, MouseEventArgs e)
           {

           }


 
    }
}
 
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