0

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 :)

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
csbalint
  • 3
  • 1

2 Answers2

0

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+"";
        }
jalsh
  • 801
  • 6
  • 18
0

You are running into an air space issue, if you can see the label you won't be able to click the button. Why not try using the labels MouseUp event to get the mouse coordinates then compare that where it positioned in the label to determine whether to increment or decrement your label.

Something like this:

private void label1_MouseUp(object sender, MouseEventArgs e)
    {
        int temp;

        if (e.Y < label1.Height / 2)
            { if (int.TryParse(label1.Text, out temp))
                label1.Text = (temp += 1).ToString();}
        else
        {
            if (int.TryParse(label1.Text, out temp))
                label1.Text = (temp -= 1).ToString();
        }
    }
Mark Hall
  • 53,938
  • 9
  • 94
  • 111