سؤال

I am currently trying to create a hover effect on a custom control, where a panel (panel1) shows up after the mouse enters the control.

I have a timer that starts when the mouseleave event is raised, the interval is 250ms, and there is an onTick event that changes the visibility of the panel1 to false.

This all works. However, the buttons on panel1 do not always respond when clicked.

Here is the pertinent code - I will supply anything else that is required if I'm missing some information.

    private void MagicCardViewer_MouseLeave(object sender, EventArgs e)
    {
        timer1.Start();
        timer1.Interval = 250;
        timer1.Tick += new EventHandler(timer1_TickOff);
        timer1.Tick -= timer1_TickOn;
    }

    private void MagicCardViewer_MouseEnter(object sender, EventArgs e)
    {
        showPanel1();
    }

    public void showPanel1()
    {
        //show necessary controls
        buttonDiscard.Show();

        //show panel1
        panel1.Visible = true;
        ActiveControl = panel1;
    }

    public void hidePanel1()
    {
        panel1.Visible = false;
        //hide controls
    }

    # region button events
    private void buttonChoose_Click(object sender, EventArgs e)
    {
        Chosen = !Chosen;

        if (Chosen)
        {
            callCardChosen();
        }
    }

    private void buttonTap_Click(object sender, EventArgs e)
    {
        cards[0].ChangeTap();

        DrawCardTap();
        onCardChanged();
    }

    private void buttonActivate_Click(object sender, EventArgs e)
    {
        cards[0].TryActivate(0);
    }

    private void buttonDiscard_Click(object sender, EventArgs e)
    {
        cards[0].onDiscard();
    }
    # endregion

I think that is everything, but there is a lot of code to select from.

breakpoints don't trigger, the button flashes but nothing happens. If the timer interval is set very long, it works fine, but the point is for the button to vanish quickly once the mouse leaves the control. If the mouse is on the buttons, then the form reports it as having left the control.

To sum up - the buttons are not always processing when I click on them.

هل كانت مفيدة؟

المحلول

If you have the timer ticking every 250ms that could prevent the event on the mouse click.

I would check that you stop the timer after the tick if you no longer need it, and restart as I think you are doing when the user leaves the control or enter it again.

This could be why it works when you set a longer time interval.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top