Reputation: 154
I have a Panel created (Windows Forms Panel), which covers a surface on a form. Panel: Panel triggerDataRefreshStudentPanel = new Panel(); I want to trigger this event (When mouse enters on panels area, fire this event)
triggerDataRefreshStudentPanel_MouseEnter();
I've set it as:
triggerDataRefreshStudentPanel.Enabled = true, triggerDataRefreshStudentPanel.Visible = false;
But, the event doesn't work, it works only if both are true. I want only the panel to be enabled, but without being visible. Practically, I want to trigger something when mouse enters in an area.. that's why I've choose to do that. Or, is there any other way of doing what I'm trying to?
Upvotes: 1
Views: 196
Reputation: 4695
A disabled and/or invisible control does not invoke a punch of input events including the mouse events. This is the idea, to disable the control's functionalities.
However, the parent control receives the mouse events when the mouse operates over a disabled and/or invisible child. Consequently, you can handle the parent's MouseMove
event to get notified whenever the mouse enters and leaves the bounds of the disabled control.
Handle the MouseMove
event of the parent control as follows:
// To avoid the redundant calls...
private bool isMouseEntered;
// `ParentControl` the parent of the disabled control. Form, Panel, GroupBox...
private void ParentControl_MouseMove(object sender, MouseEventArgs e)
{
// Optional to execute this code only when the control
// is disabled or invisible.
if (disabledControl.Enabled && disabledControl.Visible) return;
if (disabledControl.Bounds.Contains(e.Location))
{
if (!isMouseEntered)
{
isMouseEntered = true;
// Call/do here whatever you want...
}
}
else if (isMouseEntered)
{
isMouseEntered = false;
// If you have something to do when the mouse leaves
// the disabled/invisible control...
}
}
Edit
Make sure that you have added the parent's MouseMove
handler correctly. Use your debugger with a breakpoint to confirm that.
Applying the solution above to the linked code:
private bool isMouseEntered;
private void CZUMain_MouseMove(object sender, MouseEventArgs e)
{
if (triggerDataRefreshStudentPanel.Bounds.Contains(e.Location))
{
if (!isMouseEntered)
{
isMouseEntered = true;
if (_connectedUserType == "student")
{
actions = StudentsPanel.studentTriggerNewRefresh(_connectedUser, ref _studentLastMeeting, ref _studentLastAssignment, ref _studentLastColleague, ref _studentLastCourse);
refreshStudentData(actions);
//
}
else
{
StudentsPanel.updateTeachedClassesList(selectClassID, ref teachedClassesIDs, connectedUser); // updating Teacher Teached Classes List every time he clicks
}
}
}
else if (isMouseEntered)
{
isMouseEntered = false;
// ...
}
}
Upvotes: 1
Reputation: 128
Try this solution from the Link Or try to make panel with transparent background... In that way the Panel will be visible but with transparent background..
Upvotes: 0