Capture mouse event

Asked

Viewed 211 times

-1

Hey there, fellas! I’m developing a side menu that contains C# submenus, but I can’t find an effective method to display the submenus that are inside a panel. I tried with Mousemove event, however I did not get the expected result, because the panel is still visible when changing components. Could someone help me?inserir a descrição da imagem aqui

  • Hello @user welcome to Stackoverflow, please edit your question and add some code snippets so you can find the problem

  • Why not use the "menuStrip" component? It already has this function embedded to expand menus.

2 answers

1


Greetings.

I managed to do it here in a way that I think suits what you need. The events used will be the Mousehover and the Mouseleave.

I created a Panel which will serve as a lateral background, this panel will be called pnlMenuLateral. Inside the menu panel I added two Abels, one to open the submenu Sales(lblVendas) and the other to open the submenu Cadastrals(lblCadastros).

When hovering over the sales name opens the sales menu and collects any other open menu. The same will happen with the registration menu.

I added to the project 2 components of the type Timer that will assist us in visualizing the submenus. Change the range of these timers to 5. I’ll call it timerVendas and timerCasters.

Change the size of submenus panels to Width=0;

Let’s Go To The Codes:

// estas duas variáveis controlarão o tamanho das panels dos subMenus
// no meu caso aqui elas tem um whidth = 68, veja qual o tamanho total do 
// seu para que apareçam todos os componentes do subMenu
 int tamanhoVendas = 0;
 int tamanhoCadastros = 0;

// ao passar o mouse  no "lblVendas" é apresentado o submenu de vendas
private void lblVendas_MouseHover(object sender, EventArgs e)
{
    pnlCadastros.Size = new Size(pnlCadastros.Width, tamanhoCadastros);
    timerVendas.Start();// estamos escondendo o menu de cadastro
}

// parando o timer de vendas e zerando o tamanho da panel de vendas
private void lblVendas_MouseLeave(object sender, EventArgs e)
{
  timerVendas.Stop();
  tamanhoVendas = 0;
}

// aqui estamos fazendo a verificação para parar ou mostrar o subMenu de Vendas 
private void timerVendas_Tick(object sender, EventArgs e)
{
    if (tamanhoVendas > 255)
    timerVendas.Stop();
    else
    {
       pnlVendas.Size = new Size(pnlVendas.Size.Width, tamanhoVendas);
       tamanhoVendas += 5;
    }
}

Now Let’s repeat the same step for the registration menu

   private void lblCadastro_MouseHover(object sender, EventArgs e)
   {
       pnlVendas.Size = new Size(pnlVendas.Width, tamanhoVendas);
       timerCadastros.Start();
   }

    private void lblCadastro_MouseLeave(object sender, EventArgs e)
    {
        timerCadastros.Stop();
        tamanhoCadastros = 0;
    }

    private void timerCadastros_Tick(object sender, EventArgs e)
    {

        if (tamanhoCadastros > 255)
            timerCadastros.Stop();
        else
        {
            pnlCadastros.Size = new Size(pnlCadastros.Size.Width, tamanhoCadastros);
            tamanhoCadastros += 5;
        }
    }

To Finish we will make that when leaving with the mouse of the names of the submenu The Submenus themselves hide again.

I will do this twice, once for when leave with the mouse up the panelMenu and another when leaving with the mouse to the side of the main form. See what situation suits you or if equal in my case both.

   private void pnlMenu_MouseHover(object sender, EventArgs e)
     {
         pnlCadastros.Size = new Size(pnlCadastros.Width, tamanhoCadastros);
         pnlVendas.Size = new Size(pnlVendas.Width, tamanhoVendas);
     }

    private void frmEspandirMenuLateral_MouseHover(object sender, EventArgs e)
    {
        pnlCadastros.Size = new Size(pnlCadastros.Width, tamanhoCadastros);
        pnlVendas.Size = new Size(pnlVendas.Width, tamanhoVendas);
    }

It’s there, I did the tests here and it worked perfectly.

  • 1

    Thank you, it worked perfectly!

0

Greetings.

The events are the Mouseleave and Mouseenter.

first Place the submenus panels with the property Visible = false so that they start without appearing in form leading.

I’m going to assume that the menu names are components of the Labels, then when pass the mouse will show the Submenu and when leave with the mouse the Submenu vanishes (Submenu in this case = Panel).

private void lblVendas_MouseEnter(object sender, EventArgs e)
{
   pnlVendas.Visible = true;
}

private void lblVendas_MouseLeave(object sender, EventArgs e)
{
   pnlVendas.Visible = false;
}
  • That way it appears and soon comes out, it would have to be a mode in which: when the mouse was above the menu it would not disappear.

  • 1

    Endendi, I don’t know if what you intend to do is possible in Windowsforms, maybe it’s in WPF. I still find the component Menustrip more convenient.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.