C# - Mouseleave Event does not work under a different label

Asked

Viewed 146 times

0

I created two Labels with different names and created two methods for each Label responsible for making an event Hover kind of similar in CSS (was just a comparison).

//Evento responsável por fechar a aplicação através do Label "X".
private void closeApplicationClick(object sender, EventArgs e)
{    
    this.Close(); //Responsável por fechar a aplicação por meio do "X".            
}  

//MouseEvent para sublinhar o Label "Cadastrar um novo usuário"
private void newUserOnMouseEnter(object sender, EventArgs e)
{
    //Atribuição de uma nova propriedade "Underline" para que o Label "Cadastrar um novo usuário" seja sublinhado.
    newUser.Font = new Font(newUser.Font.Name, newUser.Font.SizeInPoints, FontStyle.Underline);
}

//MouseEvent para remover o sublinado do Label "Cadastrar um novo usuário"
private void newUserOnMouseLeave(object sender, EventArgs e)
{
    //Atribuição de uma nova propriedade "Regular" para que o sublinhado seja removido do Label "Cadastrar um novo usuário"
      newUser.Font = new Font(newUser.Font.Name, newUser.Font.SizeInPoints, FontStyle.Underline);
}

//MouseEvent para sublinhar o Label "Esqueci a minha senha"
private void ForgotPasswordOnMouseEnter(object sender, EventArgs e)
{
    //Atribuição de uma nova propriedade "Underline" para que o Label "Esqueci a minha senha" seja sublinhado.
      forgotPassword.Font = new Font(forgotPassword.Font.Name, forgotPassword.Font.SizeInPoints, FontStyle.Underline);
}

//MouseEvent para remover o sublinado do Label "Esqueci a minha senha"
private void ForgotPasswordOnMouseLeave(object sender, EventArgs e)
{
    //Atribuição de uma nova propriedade "Regular" para que o sublinhado seja removido do Label "Esqueci a minha senha"
      forgotPassword.Font = new Font(forgotPassword.Font.Name, forgotPassword.Font.SizeInPoints, FontStyle.Regular);
}

The two methods have different names, I tried to change the variable of the object sender but it didn’t work until EventArgs e I switched to another variable and it didn’t work either.

I thought it would work perfectly but only the I forgot my password is working perfectly.

I already setei the events MouseEnter and MouseLeave Label properties, but does not work.

Check out this image:

inserir a descrição da imagem aqui

  • It’s for desktop... or should I say Windows Forms Application, is not web application.

  • Where you are adding the Mouseenter and Mouseleave Vents to the respective linklabel?

  • Wouldn’t it be better to use "Mouseover"?

  • @Patrick I also thought that it worked kind of like the CSS kkk but did not give.

1 answer

0

Israel, I suggest centering on one method the action of underlining (or styling generally), so what works for one, will work for others, besides of course, of keeping the code cleaner for revisions:

forgotPassword.MouseEnter += (sender, e) => EstilizaLink(sender, e, "sublinhar");

or

forgotPassword.MouseLeave += (sender, e) => EstilizaLink(sender, e, "naoSublinhar");

And the method EstilizaLink(...):

void EstilizaLink(object sender, EventArgs e, string acao)
{
    var label = ((Label) (sender));
    var estilo = (acao == "sublinhar" ? FontStyle.Underline : FontStyle.Regular);

    label.Font = new Font(label.Font.Name, label.Font.SizeInPoints, estilo);
}

Obviously the method may bring other characteristics. Anyway, I tested it here and had no problem to underline and remove the underscore in mouseEnter and mouseLeave.

  • What do you mean center on only one method? Anyway you won’t be using one method to set the event and another for Stylizatlink?

  • Conceptually speaking, "stylize" can be stored in a single method or, depending on the volume included, a static class with static methods (since this would not need to be instantiated). Did you even test? For me it worked, I put in 2 Labels.

  • I tried to, but Visual Studio reports an error in the Sender variable when I call forgotPassword.Mouseleave += (Sender, e) => Stylize Link(Sender, e, "naoSublinhar");

  • What mistake? Remembering that forgotPassword.Mouseleave += (...) must be called no . Cs and must have access to the function scope void Stylized(...) that we just created.

Browser other questions tagged

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