Home > Software design >  Cannot keep OnClick event after visibilty change
Cannot keep OnClick event after visibilty change

Time:01-08

Just wondering if someone else can recreate this or is it just me :)

  • Drop a panel on the form
  • Put a button on the panel
  • Create click of button "I am Button"

Above works fine

  • Add onm ouseleave to panel set button visible to false
  • Add onm ouseenter to panel set button visible to true

Now if you move mouse in and out of panel the button will become visible false. Pefect.

Now move mouse back intp panel and button becomes visible. Perfect.

Clicking on button now does nothing (Lost on Click Event?)

Breakpoint in onclick never fires.

Is this a concept that cannot be done?

'Update' It wont let me post long comment.

First of all thanks for the quick responses.

I did not include code because this was in a very large project so i recreated it with a brand new app.

C# WinForm with one panel and one button on the panel.

I did not try to click on an invible button. Not only can i see the button but can see it respond to bing clicked.

@steve. Good point, The button is well inside of the panel but i see where you are going. I just performed a new test.

If i move the mouse in and out of the panel the button will show and hide perfectly. After this, hitting enter on the button will excute onclick but the mouse will not. (I suppose this has something to do with only one button and it is default.)

What prompts the mouse to not fire the click event is beyond me. Very Strange.

What i am trying to achieve: An area on the screen that when the mouse enters this area a group of buttons appear so they can be used. when the mouse leaves this area the buttons will dissapear. As my original post stated button works fine if visiblity does not change.

CodePudding user response:

I suppose the your code has the following problem:

  • You receive the mouseenter event on the panel and you make the button visible
  • You move the mouse over the button

This last action triggers the mouseleave event on the panel.
As a consequence your button becomes invisible.
But making the button invisible triggers a mouseenter over the panel and in a blink the button is again visible and causing a mouseleave on the panel.
An infinite loop between these two events starts.

So the click event is not lost, simply it will never trigger because the form engine is busy hiding and showing the button control.

I have reproduced the situation and fixed it adding an event handler for the MouseMove event. In this event I set a variable to block the infinite loop between the two events...

This is my example to adapt to your real code, try it in LinqPad

public class Form1 : Form
{ 
    Button b1 = new Button();
    Panel p1 = new Panel();
    bool stillOnPanel = false;

    public Form1()
    {
        b1.Text = "ClickMe";
        b1.Click  = onClick;
        b1.Visible = false;

        p1.MouseEnter  = onEnter;
        p1.MouseLeave  = onLeave;
        p1.MouseMove  = onMove;
        p1.BorderStyle = BorderStyle.FixedSingle;
        p1.Size = new Size(150,100);
        this.Controls.Add(p1);
        p1.Controls.Add(b1);
    }
    void onClick(object sender, EventArgs e)
    {
        Console.WriteLine("Clicked");
    }
    void onMove(object sender, MouseEventArgs e)
    {
        //Check if we are still over the panel area
        stillOnPanel = b1.ClientRectangle.Contains(p1.PointToClient(Cursor.Position));
    }
    void onEnter(object sender, EventArgs e)
    {
        if (!b1.Visible)
        {
            b1.Visible = true;
        }
    }
    void onLeave(object sender, EventArgs e)
    {
        // Do not hide the button if we are over it, let it click....
        if (b1.Visible && !stillOnPanel)
        {
            b1.Visible = false;
        }
    }
}

CodePudding user response:

@Steve, Thank you.

This line did it and it makes sense now.

stillOnPanel = panel1.ClientRectangle.Contains(panel1.PointToClient(Cursor.Position));

Again, Thanks @Steve.

RESOLVED

  •  Tags:  
  • Related