4

I have a problem regarding changing appearance of a label. Here is the screenshot:

enter image description here

That is the color when you hover the mouse and I want it like that. What I want is for it to stay that color when I clicked it. But because of my mouseleave control it would not work like I want it.

Here is the code:

   private void btnArchives_MouseEnter(object sender, EventArgs e)
    {
        lblArchives.BackColor = Color.FromArgb(9, 18, 28); //darkercolor
    }

   private void btnArchives_MouseLeave(object sender, EventArgs e)
   {
       lblArchives.BackColor = Color.FromArgb(15, 34, 53); //lightercolor
   }

I tried mouse hover too. It looked the same as mouse enter though. Bottom line is I want the color to change to the darker color when hovered over them and change back to the lighter color when hovered out of them. But I also what it to stay dark color when I clicked it. And then turn back to lighter color then I click another button and that other button will now turn to darker color. Thank you!

EDIT: i used label instead of buttons. Im currently trying some of the comments below thank you very much.

FutureDev
  • 145
  • 3
  • 3
  • 19

5 Answers5

4

I use this technique, just tested it and I think its what you want.

    Label clickedLabel;
    private void mouseEnter(object sender, EventArgs e)
    {
        Label theLabel = (Label)sender;
        if (theLabel != clickedLabel)
            theLabel.BackColor = Color.Red;
    }

    private void mouseLeave(object sender, EventArgs e)
    {
        Label theLabel = (Label)sender;
        if (theLabel != clickedLabel)
            theLabel.BackColor = Color.Yellow;
    }

    private void labelClick(object sender, EventArgs e)
    {
        setColor();//Calling this here so clickedLabel is still the old value
        Label theLabel = (Label)sender;
        clickedLabel = theLabel;
    }

    public void setColor()
    {
        if(clickedLabel != default(Label))
            clickedLabel.BackColor = Color.Yellow;
        //Resetting clicked label because another (or the same) was just clicked.
    }

Explanation:
These events are only set to labels so we can do (Label)sender which means label that activated the event. I made a Label clickedLabel variable and set it to the clicked label, as soon as the other is clicked the variable will change and the checks will work.

Best thing about this method is it doesn't matter how many Labels you have, you never reference them as name only as sender.

EpicKip
  • 4,015
  • 1
  • 20
  • 37
  • I used label sir but the name is btn. What do should i edit in this code sir? shall i changed the "theLabel" to the name of my label? – FutureDev Feb 06 '17 at 09:23
  • @FutureDev The part under EDIT tells you what to change, give me a minute ill change it. – EpicKip Feb 06 '17 at 09:24
  • I got it to work sir but it has one problem. When i clicked label1 it works good. The problem is when i clicked label2, label1 still has the dark colors. So they both got dark colors now. It only dissapear when you hover on and out of label1. – FutureDev Feb 06 '17 at 09:39
  • @FutureDev Sorry had a dev meeting, add a method, call it `setColor` (example) and set the color of all your buttons to the normal color then call that method at the top of the click – EpicKip Feb 06 '17 at 10:11
  • @FutureDev Made a few changes to fix the issue you were having. – EpicKip Feb 06 '17 at 10:20
  • I created a method with the default colors on it and put it on the top code inside the click method but now its not working. Its not darkening the color when i click it anymore. – FutureDev Feb 06 '17 at 10:25
  • @FutureDev Use the piece of code i added, only change clickedButton to default :) You can just use that one line at the top of the click event you dont have to use a method. – EpicKip Feb 06 '17 at 10:26
  • Okay sir ill try the new code now. but why did you convert it to button i am using label. – FutureDev Feb 06 '17 at 10:29
  • @FutureDev Oh sorry I thought you said you were using a button I misread – EpicKip Feb 06 '17 at 10:30
  • Its not working sir i did not use the new code because it on button. i used the last code on label. Even when i put the method for default colors on top of the click codes. It is still making the label to default color. So the label is not darkening when clicked. or in your code, its not turning to red when click – FutureDev Feb 06 '17 at 10:33
  • @FutureDev Made a (hopefully) final edit. I think this is what you need. It works for labels on my own test. – EpicKip Feb 06 '17 at 10:37
  • It really is the final edit sir. Its working perfect now thank you! – FutureDev Feb 06 '17 at 10:45
  • @FutureDev Glad I could help, thanks for accepting. :) – EpicKip Feb 06 '17 at 10:46
  • do you mind looking at my new question? Sorry if its bothering but you have been very patient in helping me. thanks http://stackoverflow.com/questions/42162665/multiple-values-in-one-field-foreign-keys/42162785#42162785 – FutureDev Feb 10 '17 at 15:32
  • @FutureDev On my way from work to home, I might be able to help you in 2 hours. Always happy to help btw :) – EpicKip Feb 10 '17 at 15:43
  • Thank you sir epickip. Have a safe trip! – FutureDev Feb 10 '17 at 15:46
  • Sir @epickip you forgot about me hehe – FutureDev Feb 11 '17 at 02:40
  • @FutureDev Sorry mate 4am had a party get to you asap :) – EpicKip Feb 11 '17 at 02:54
2

You can remove the event handlers btnArchives_MouseLeave and button1_MouseEnter when button clicked to prevent it. But you need to add it back when the button is clicked again:

private void btnArchives_Click(object sender, EventArgs e)
{
            if (!clicked)
            {
                btnArchives.MouseEnter-= new EventHandler(btnArchives_MouseEnter);
                btnArchives.MouseLeave-= new EventHandler(btnArchives_MouseLeave);
                clicked = true;
                return;
            }

            btnArchives.MouseEnter += new EventHandler(btnArchives_MouseEnter);
            btnArchives.MouseLeave += new EventHandler(btnArchives_MouseLeave);
            clicked = false;
  }

  void btnArchives_MouseLeave(object sender, EventArgs e)
  {
              this.btnArchives.BackColor = Color.FromArgb(15, 34, 53);
  }

  void btnArchives_MouseEnter(object sender, EventArgs e)
  {
              this.btnArchives.BackColor = Color.FromArgb(9, 18, 28);
  }
elirandav
  • 1,913
  • 19
  • 27
  • If its not much can you show me how to do that? Thanks – FutureDev Feb 06 '17 at 09:01
  • @FutureDev this might help you http://stackoverflow.com/questions/16259413/how-to-remove-and-re-attatch-eventhandler-to-controls-in-c – Dillanm Feb 06 '17 at 09:02
  • @FutureDev, i added example – elirandav Feb 06 '17 at 09:09
  • im getting an error on the "cliced" sir. shall i declare a "bool clicked;" ? – FutureDev Feb 06 '17 at 09:42
  • Yes. Declare a bool field named "clicked" and initialize it with false. Sorry for not adding it. – elirandav Feb 06 '17 at 09:50
  • i got it to worked sir but there is a problem. When i clicked LabelA it gets darker like i wanted, The problem is when i click LabelB it is not getting darker and LabelA still dark. In order for LabelB to get darker when clicked is to click LabelA again. – FutureDev Feb 06 '17 at 10:01
  • The example is to show you that you can disable logic by removing handlers, and enabling by adding them back. if it is not enough to solve your problem, can you please share all scenarios for the two labels? – elirandav Feb 06 '17 at 10:11
  • Okay there are 2 labels. Label A and Label B. I want to darken their color when i hover over them like screenshot above. When i click Label A, i want its color to be darker and Label B is default(lighter). Then when i click Label B, Label A should be lighter(default) and Label B should be darker. – FutureDev Feb 06 '17 at 10:18
2

maybe add a 'if' in btnArchives_mouseLeave EventHandler like:

{
     if(!btnArchives.IsClicked())
     {
          btnArchives.BackColor = Color.FromArgb(15, 34, 28);//lightercolor
     }
}

or: remove EventHandler on PressEvent

  private void btnArchives_MouseClick(object sender, EventArgs e)
  {
    btnArchives.BackColor = Color.FromArgb(9, 18, 28); //darkercolor
    btnArchives.OnMouseLeave-= btnArchives_MouseLeave
  }

i like the first..

leiming
  • 21
  • 2
  • Isclicked is not implements by button class .but you can change to another method,you can add something to the button when you click it,and check it when the MouseLeave Event raise. – leiming Feb 06 '17 at 09:11
  • There is no "isclicked" control when i type it sir. maybe bacause is used label instead of button? – FutureDev Feb 06 '17 at 09:33
1

You can save your selected Label in the OnClick event. And check in the other methods if it's selected.

Something like this:

private void label_MouseClick(object sender, MouseEventArgs e)
{
    var label = (Label)sender;
    if (label == this.selectedLabel || this.selectedLabel == null) return;

    this.selectedLabel.BackColor = Color.FromArgb(15, 34, 53); //lightercolor
    this.selectedLabel= label;
}

private void label_MouseLeave(object sender, EventArgs e)
{
    var label = (Label)sender;
    if (label == this.selectedLabel || this.selectedLabel == null) return; 
    label.BackColor = Color.FromArgb(15, 34, 53); //lightercolor
}

private void label_MouseEnter(object sender, EventArgs e)
{
    var label = (Label)sender;
    label.BackColor = Color.FromArgb(9, 18, 28); //darkercolor
}

Note, that this is generic and you should add the same methods to all your labels
This will also make sure that when you click one label the other one will be deselected.

In your class just add a private Label selectedLabel

Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
  • I am having an error on the this.SelectedLabel = sender; the "Sender" is underlined red with an error "cannot convert object to label" – FutureDev Feb 06 '17 at 09:29
  • im getting an error on this line "this.selectedLabel.BackColor = Color.FromArgb(15, 34, 53); //lightercolor" the error is " Object reference not set to an instance of an object." – FutureDev Feb 06 '17 at 09:51
  • Sorry, Added a null check now – Ofir Winegarten Feb 06 '17 at 10:31
0
private string activeLabel;

private void btnArchives_Click(object sender, EventArgs e)
    {
        activeLabel = btnArchives.Text;
    }

private void btnArchives_MouseEnter(object sender, EventArgs e)
    {
        lblArchives.BackColor = Color.FromArgb(9, 18, 28); //darkercolor
    }

private void btnArchives_MouseLeave(object sender, EventArgs e)
{

    if (activeLabel = btnArchives.Text)
    {
       lblArchives.BackColor = Color.FromArgb(9, 18, 28); //darkercolor
    }
    else
    {
       lblArchives.BackColor = Color.FromArgb(15, 34, 53); //lightercolor
    }

}

There is no function to check if the label is clicked therefore, you use a global string variable to collect the label currently clicked. And then make the forecolor darker if the label is the currently clicked else return back to the lightercolor.

kennyozordi
  • 21
  • 1
  • 2