mouse event in C #

3

I have a panel and inside the panel I have 12 buttons, which are invisible, I want that when mouse enters with the mouse panelEntered I expand a bit and show the buttons and when it comes out with mouseLeave go back to its normal form and hide.

private void cancha1_MouseEnter(object sender, EventArgs e)
    {



        cancha1.SetBounds(15, 18, 339, 174);

        foreach (Control bt in this.cancha1.Controls)
        {
            bt.Visible = true;
        }
        foreach (Control bt in this.cp2.Controls)
        {
            bt.Visible = true;
        }
    }

    private void cancha1_MouseLeave(object sender, EventArgs e)
    {


        cancha1.SetBounds(16, 19, 328, 163);
        foreach (Control bt in this.cancha1.Controls)
        {
            bt.Visible = false;
        }
        foreach (Control bt in this.cp2.Controls)
        {
            bt.Visible = false;
        }
    }

The error is that when I enter the panel if I expand it and show it to me, but when I go over a button (without leaving the panel) it makes me small. I do not know how to do it so they do not change it. it is as if when you enter the component you will notice that it has left your container.

    
asked by srJJ 29.03.2018 в 04:39
source

2 answers

2

There are several solutions, I will expose one here. What you can do is check in your event MouseLeave if the mouse is within the limits of the panel. To do this, you can use the property Bounds that have all the controls, and check if the position of the mouse is within those limits:

private void cancha1_MouseLeave(object sender, EventArgs e)
{
    if (!cancha1.Bounds.Contains(this.PointToClient(Cursor.Position)))
    {
        cancha1.SetBounds(16, 19, 328, 163);
        foreach (Control bt in this.cancha1.Controls)
        {
            bt.Visible = false;
        }
        foreach (Control bt in this.cp2.Controls)
        {
            bt.Visible = false;
        }
    }
}

If you notice, to get the mouse position, use Cursor.Position , which returns the position of the mouse on the screen. Since we need the position of the mouse inside the form, we use the method PointToClient , which returns the coordinates within the form.

    
answered by 29.03.2018 в 12:05
0

Look @Pikoh this the code that I put after you helped me, because I have an error that if you entered through the inner part of the panel the buttons come in with a kind of infinite cycle that starts to go in and out, enlarging and dwarfing the panel.

private void cancha1_MouseEnter(object sender, EventArgs e)
    {

        Console.WriteLine("entro   ");

        cancha1.SetBounds(15, 18, 339, 174);

        foreach (Control bt in this.cancha1.Controls)
        {
            bt.Visible = true;
        }
        foreach (Control bt in this.cp1.Controls)
        {
            bt.Visible = true;
        }
    }

    private void cancha1_MouseLeave(object sender, EventArgs e)
    {
        //Console.WriteLine("salio   " + Cursor.Position);
        //Console.WriteLine("salio22   " + Bounds.Contains(this.PointToClient(Cursor.Position)));

        if (cancha1.Bounds.Contains(cancha1.PointToClient(Cursor.Position)))
        {
            cancha1.SetBounds(16, 19, 328, 163);
            foreach (Control bt in cancha1.Controls)
            {
                bt.Visible = false;
            }
            foreach (Control bt in cp1.Controls)
            {
                bt.Visible = false;
            }
        }

    }

    
answered by 31.03.2018 в 21:09