简体   繁体   中英

Restrict clickable area of a checkbox control in winforms

I created a custom checkbox control and inherited the class from System.Windows.Forms.CheckBox.I set the dock property of this control as Top. I added some text to the control. The control has some empty space. Now if click on the empty space, this check box is working. I need to restrict the empty space from clickable area.

The below is what i tried,

public class MyCheckBox : System.Windows.Forms.CheckBox
{
    private int boxWidth = 15;

    protected override void OnClick(EventArgs e)
    {
        Point mouseLocation = this.PointToClient(Control.MousePosition);
        if (mouseLocation.X > boxWidth)
            return;

        base.OnClick(e);
    }
}

This is working fine when clicking on the box of the checkbox control. But i need to add the text area too to the clickable area.

如果您希望您的用户玩“复选框狙击手”(出于某种原因,可点击区域很大),您可以考虑只放置一个没有文本的复选框,将其调整为尽可能小的尺寸并使用单独的标签显示文本。

If you don't want the empty area to be "clickable", then don't dock it to the top of the form. If you dock a standard CheckBox at the top, you'll see the same effect. The hit area becomes the entire width of the form.

Just place the control at the top of the screen with Dock set to None .

Or dock a Panel to the top of the screen, then place your custom CheckBox control inside that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM