Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am having several labels in my tab control, initially their font will be in regular, but i need to make one of the label BOLD when mouse entered on a particular label. we can write Mouse Enter event and Mouse Leave event for each and every label. But i need only a few lines of code, in which i need to make the Label Bold when my mouse enters on a particular label.
i dont need to write these events to each and every label.
Can any one suggest?

What I have tried:

private void linkLabel24_MouseEnter(object sender, EventArgs e)
        {
            linkLabel24.Font = new Font(linkLabel24.Font.Name, 10, FontStyle.Bold);
        }


private void linkLabel24_MouseLeave(object sender, EventArgs e)
       {
           linkLabel24.Font = new Font(linkLabel24.Font.Name, 10, FontStyle.Regular);
       }
Posted
Updated 22-Jan-18 18:42pm

1 solution

try

private void Form1_Load(object sender, EventArgs e)
      {
          foreach (TabPage tab in tabControl1.Controls)
          {
              foreach (Label lbl in tab.Controls.OfType<Label>())
              {
                  lbl.MouseEnter += lbl_MouseEnter;
                  lbl.MouseLeave += lbl_MouseLeave;
              }
          }
      }

      void lbl_MouseLeave(object sender, EventArgs e)
      {
          Label lbl = (Label)sender;
          lbl.Font = new Font(lbl.Font.Name, 10, FontStyle.Regular);
      }

      void lbl_MouseEnter(object sender, EventArgs e)
      {
          Label lbl = (Label)sender;
          lbl.Font = new Font(lbl.Font.Name, 10, FontStyle.Bold);
      }
 
Share this answer
 
Comments
chaitanya556 23-Jan-18 1:19am    
in tab.Controls.OfType() OfType is not coming
Karthik_Mahalingam 23-Jan-18 2:03am    
add this
using System.Collections.Generic;
chaitanya556 23-Jan-18 2:26am    
I already having that
Karthik_Mahalingam 23-Jan-18 2:40am    
Usung System.linq;
chaitanya556 23-Jan-18 3:13am    
Hi, Using Linq has worked. but my controls are even more than tab control, there are panels and list items. i tried your code it build was succeeded but no changing of font style was there. so i tried making the Mouse leave and Mouse enter events
void lbl_MouseLeave(object sender, EventArgs e)
{
Label lbl = (Label)sender;
lbl.Font = new Font(lbl.Font.Name, 10, FontStyle.Regular);
}

void lbl_MouseEnter(object sender, EventArgs e)
{
Label lbl = (Label)sender;
lbl.Font = new Font(lbl.Font.Name, 10, FontStyle.Bold);
}
as public. and pasted these events in the properties of labels in design form. it worked.

Thank you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900