Search code examples
c#winformsbuttonlabelinvisible

How can I create an Invisible (and clickable) button behind a label?


I'm trying to make an invisible button behind the Label.

This is what I achieved so far.

The idea behind this, is if I click the lower part of a Number , it should decrease, If i click the upper part of it, it should increase, this is what I want to achieve.

This is how I made my button invisible:

button2.FlatStyle = FlatStyle.Flat; 
button2.FlatAppearance.BorderSize = 0; 
button2.FlatAppearance.MouseDownBackColor = Color.Transparent; 
button2.FlatAppearance.MouseOverBackColor = Color.Transparent; 
button2.BackColor = Color.Transparent; 

The only problem with this, is if I move my button to the Label, it hides the Label. (I tried to 'Send to back' the button, but when I did it, it wasn't clickable anymore.)

If you have a solution please share it with me :)


Solution

  • Bubbling up events is not supported by standard in winforms, it's available by default in WPF, an easier solution for your issue is to handle the MouseClick event of your label

        private void numLabel_MouseClick(object sender, MouseEventArgs e)
            {
                int num = 0;
                int.TryParse(numLabel.Text, num);
                if (e.Y > numLabel.Size.Height / 2) num--; else num++;
                numLabel.Text = num+"";
            }