With this code you can add panels and move them ONLY in THE Panel they are deployed.
//Limitation Code////::START::::
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
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);
}
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();
}
private void add_Task_Button_Click(object sender, EventArgs e)
{
AddTask();
}
private void AddTask()
{
TaskPanel = new Panel();
TaskPanel.Location = new System.Drawing.Point(30, 50);
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";
TaskPanel.MouseDown += new MouseEventHandler(TaskPanel_MouseDown); TaskPanel.MouseMove += new MouseEventHandler(TaskPanel_MouseMove); TaskPanel.MouseUp += new MouseEventHandler(TaskPanel_MouseUp); TaskPanel.LocationChanged += new System.EventHandler(TaskPanel_LocationChanged);
TaskPanel.KeyDown += MovingPanel_KeyDown;
TaskPanel.MouseClick += new MouseEventHandler(TaskPanel_MouseClick);
loader_panel.Controls.Add(TaskPanel);
activeControl = TaskPanel;
}
private void TaskPanel_MouseDown(object sender, MouseEventArgs e)
{
activeControl = sender as Control;
previousPosition = e.Location;
Cursor = Cursors.Hand;
}
private void TaskPanel_MouseMove(object sender, MouseEventArgs e)
{
if (activeControl == null || activeControl != sender)
{
return;
}
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
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);
}
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;
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)
{
}
}
}