I apologize in advance if this has been asked already. Searching stackoverflow and the internet didn't present any helpful examples.
In the running program, I have a picture box I need to be able to click and drag from one GroupBox to another. I know how to drag the picture box around the WinForm itself (i.e. without any GroupBoxes involved). I can't find any examples of how to do this.
I've created code for every event it seems would be relevant to group box / mouse interaction (see end of post for code).
Note: Thanks for the feedback on needing the code. I'm new Stack Overflow.
INTERFACE: Initial Interface
PROBLEM: When I click and drag pbxMoveIt over the group boxes, the groupbox events never fire, based on the text in the status label.
Dragging image over group box doesn't trigger group box enter, hover or other events
When I move the mouse without dragging anything, the group box mouse hover event fires.
Group box mouse hover event fires when not dragging anything.
ADDED Code:
namespace MoveControlsOnFormBetweenGroupBoxes {
public partial class frmMain : Form {
private Point m_MouseDownLocation;
private bool m_IsDragging;
public frmMain ( ) {
InitializeComponent ( );
pbxMoveIt.BringToFront ( );
gbx1.AllowDrop = true;
gbx2.AllowDrop = true;
lblStatus.Text = "GUI Status: Started";
}
#region Picture Box Related Methods
// Picture Related Methods
private void pbxMoveIt_MouseDown ( object sender, MouseEventArgs e ) {
lblStatus.Text = "GUI Status: pbxMoveIt - MouseDown";
if ( e.Button == MouseButtons.Left ) {
m_MouseDownLocation = e.Location;
m_IsDragging = true;
}
}
private void pbxMoveIt_MouseMove ( object sender, MouseEventArgs e ) {
int newX;
int newY;
int minX = 10;
int minY = 10;
int maxX = this.Width - (25 + pbxMoveIt.Width);
int maxY = this.Height - (45 + pbxMoveIt.Height);
if ( e.Button == MouseButtons.Left ) {
lblStatus.Text = "GUI Status: pbxMoveIt - MouseMove";
newX = e.X + pbxMoveIt.Left - m_MouseDownLocation.X;
newY = e.Y + pbxMoveIt.Top - m_MouseDownLocation.Y;
if ( m_IsDragging ) {
if ( ( newX >= minX ) && ( newX <= maxX ) ) {
pbxMoveIt.Left = newX;
}
if ( ( newY >= minY ) && ( newY <= maxY ) ) {
pbxMoveIt.Top = newY;
}
}
}
}
private void pbxMoveIt_MouseUp ( object sender, MouseEventArgs e ) {
lblStatus.Text = "GUI Status: pbxMoveIt - MouseUp";
if ( e.Button == System.Windows.Forms.MouseButtons.Left ) {
m_IsDragging = false;
}
}
#endregion
#region Group Box Related
// Group Box Related Methods
private string Gbx_Title ( object sender ) {
string boxTitle = "Unknown";
if ( sender == gbx1 ) {
boxTitle = "Group Box 1";
}
if ( sender == gbx2 ) {
boxTitle = "Group Box 2";
}
return boxTitle;
}
private void gbx_DragDrop ( object sender, DragEventArgs e ) {
lblStatus.Text = String.Format ( "GUI Status: {0} - DragDrop", Gbx_Title ( sender ) );
}
private void gbx_DragEnter ( object sender, DragEventArgs e ) {
lblStatus.Text = String.Format ( "GUI Status: {0} - DragEnter", Gbx_Title ( sender ) );
}
private void gbx_DragLeave ( object sender, EventArgs e ) {
lblStatus.Text = String.Format ( "GUI Status: {0} - DragLeave", Gbx_Title ( sender ) );
}
private void gbx_DragOver ( object sender, DragEventArgs e ) {
lblStatus.Text = String.Format ( "GUI Status: {0} - DragOver", Gbx_Title ( sender ) );
}
private void gbx_Enter ( object sender, EventArgs e ) {
lblStatus.Text = String.Format ( "GUI Status: {0} - Enter", Gbx_Title ( sender ) );
}
private void gbx_Leave ( object sender, EventArgs e ) {
lblStatus.Text = String.Format ( "GUI Status: {0} - Leave", Gbx_Title ( sender ) );
}
private void gbx_MouseHover ( object sender, EventArgs e ) {
lblStatus.Text = String.Format ( "GUI Status: {0} - MouseHover", Gbx_Title ( sender ) );
}
#endregion
}
}