2

Is there a way to change the "hit box" of a click event in c#, caused by clicking a button or another control?

I am using custom GUI controls to control a device. These controls work as small circles both indicating current status (green or black) and as buttons that the user can click.

The problem is, i am using a touch screen on the device, and it is not so easy to click the small circles. 50% of the time you miss it and the controls click event is not raised.

I want to change this without changing the visual size of the circles, as my GUI is already limited on space. So i want an invisible clickable hit box that is just slightly larger than the control itself. Is it possible?

A.Force
  • 73
  • 6
  • You can solve the problem in multiple ways, for example: **♦** You can use a [flat button](http://stackoverflow.com/a/32966147/3110834), having `FlatStyle` set to `Flat` and use the same `BackColor`, set `FlatAppearance.MouseDownBackColor` and `FlatAppearance.MouseOverBackColor` to a suitable color and use desired image for button. **♦** You can Set region of button to a round region and draw the circle a little smaller. **♦** Or may be you want to use a [circular radio button list](http://stackoverflow.com/a/38390028/3110834). – Reza Aghaei Nov 24 '16 at 14:34

3 Answers3

1

Use a Panel while designing your UserControl then place clickable item inside the panel, a Panel will be transparent by default. You don't have to set any other settings to it, and if user clicks the panel fire control1_Click event.

private void control1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Test");
}

private void panel1_Click(object sender, EventArgs e)
{
    control1_Click(sender, e);
}
abdul
  • 1,562
  • 1
  • 17
  • 22
1

Thanks for the tips all! What i ended up doing was similar to both Abdul and Active92's answers.

I created a picturebox for each button, made it bigger than the button itself and placed it "behind" the button. (Right click and send to back). Since i dont add any images the pictureboxes are invisible.

In each picturebox click event handler i just fire a click event of the original control.

A.Force
  • 73
  • 6
0

Create a bigger button and do something like the code below where button1 is your original button and button2 is the bigger button.

  private void button1_Click(object sender, EventArgs e)
    {
        ShowMessage();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        ShowMessage();
    }

    private void ShowMessage()
    {
        MessageBox.Show("ok", "ok");
    }

Set button2's text as blank.

Set FlatStyle to flat.

Set BorderColor, MouseDownBackColor and MouseOverBackColor to BackColor.

Right click button2 and send to back.

active92
  • 644
  • 12
  • 24