Home > Mobile >  Trigger an event for an invisible Panel - .Net
Trigger an event for an invisible Panel - .Net

Time:12-24

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?

CodePudding user response:

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

CodePudding user response:

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...
    }
}
  • Related