Note: Question Title and text changed. I realized I had asked the wrong question for my needs.
I have a problem I can't solve. I have a picturebox on a user control:
Clicking on the parent allows me to drag the entire control (I can click and drag it anywhere on the WinForm).
I need to be able to drag the parent control by clicking and dragging the picture box (child control).
The picturebox should never be moved within the parent control. Clicking the child control and dragging needs to move the parent and the children controls without changing their position within the parent control.
I've tried putting together what I've seen online, but I'm missing something. The code below has separate events in the WinForm for handling the user control and user control's children.
public partial class frmMain : Form {
private Point m_MouseDownLocation;
private bool m_IsDragging;
public frmMain ( ) {
InitializeComponent ( );
suc1.MouseDown += SimpleUserControl_MouseDown;
suc1.MouseMove += SimpleUserControl_MouseMove;
suc1.MouseUp += SimpleUserControl_MouseUp;
suc1.PbxMoveIt.MouseDown += SimpleUserChildControl_MouseDown;
suc1.PbxMoveIt.MouseMove += SimpleUserChildControl_MouseMove;
suc1.PbxMoveIt.MouseUp += SimpleUserChildControl_MouseUp;
}
#region SimpleUserControl Related
private void SimpleUserControl_MouseDown ( object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Left ) {
m_MouseDownLocation = e.Location;
m_IsDragging = true;
suc1.DisableButton ( );
}
}
private void SimpleUserControl_MouseMove ( object sender, MouseEventArgs e ) {
int newX;
int newY;
int minX = 10;
int minY = 10;
int maxX = this.Width - (25 + suc1.Width);
int maxY = this.Height - (45 + suc1.Height);
if ( e.Button == MouseButtons.Left ) {
newX = e.X + suc1.Left - m_MouseDownLocation.X;
newY = e.Y + suc1.Top - m_MouseDownLocation.Y;
if ( m_IsDragging ) {
if ( ( newX >= minX ) && ( newX <= maxX ) ) {
suc1.Left = newX;
}
if ( ( newY >= minY ) && ( newY <= maxY ) ) {
suc1.Top = newY;
}
}
}
}
private void SimpleUserControl_MouseUp ( object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Left ) {
m_IsDragging = false;
suc1.EnableButton ( );
}
}
#endregion
#region Simple User Child Control Related
private void SimpleUserChildControl_MouseDown ( object sender, MouseEventArgs e ) {
SimpleUserControl useThis = (SimpleUserControl)((Control)sender).Parent;
if ( e.Button == MouseButtons.Left ) {
m_MouseDownLocation = e.Location;
m_IsDragging = true;
useThis.DisableButton ( );
}
}
private void SimpleUserChildControl_MouseMove ( object sender, MouseEventArgs e ) {
SimpleUserControl useThis = (SimpleUserControl)((Control)sender).Parent;
int newX;
int newY;
int minX = 10;
int minY = 10;
int maxX = useThis.Width - (25 + useThis.Width);
int maxY = useThis.Height - (45 + useThis.Height);
if ( e.Button == MouseButtons.Left ) {
newX = e.X + useThis.Left - m_MouseDownLocation.X;
newY = e.Y + useThis.Top - m_MouseDownLocation.Y;
if ( m_IsDragging ) {
if ( ( newX >= minX ) && ( newX <= maxX ) ) {
useThis.Left = newX;
}
if ( ( newY >= minY ) && ( newY <= maxY ) ) {
useThis.Top = newY;
}
}
}
if ( e.Button == MouseButtons.Right ) {
MessageBox.Show ( "Right Button Clicked!" );
}
}
private void SimpleUserChildControl_MouseUp ( object sender, MouseEventArgs e ) {
SimpleUserControl useThis = (SimpleUserControl)((Control)sender).Parent;
if ( e.Button == MouseButtons.Left ) {
m_IsDragging = false;
useThis.EnableButton ( );
}
}
#endregion
}