0

我正在制作一个配置编辑器表单并且遇到了一些问题,我花了很多时间进行用户友好和高效的设计,因此希望它TabIndex能够完美地工作以尽量减少鼠标的使用。

我现在的问题是,当我尝试通过控件切换时,我注意到CheckBox没有像用鼠标按下它那样获得焦点,这意味着我无法直接从键盘切换并更改它们的状态。

如何通过andCheckBox获得焦点,这样我只需按 Enter 键即可通过 KeyUp 事件更改其状态。 TabIndexTabStop

下面是我的表格的图片,旁边是一张图片TabIndex以及直接取自Form.Designer.cs班级的代码。

在此处输入图像描述

        // 
        // cbxDefaultPublic
        // 
        this.cbxDefaultPublic.AutoSize = true;
        this.cbxDefaultPublic.Location = new System.Drawing.Point(247, 12);
        this.cbxDefaultPublic.Name = "cbxDefaultPublic";
        this.cbxDefaultPublic.Size = new System.Drawing.Size(15, 14);
        this.cbxDefaultPublic.TabIndex = 1;
        this.cbxDefaultPublic.TabStop = true;
        this.cbxDefaultPublic.UseVisualStyleBackColor = true;

请注意,我很难解释这个原因,因为它有点复杂,如果我弄错了一些事情,我不知道如何解释它。

4

1 回答 1

0

在人们对我的问题发表评论的帮助下,我能够正确地了解要做什么和要搜索什么。
感谢 Grant Winney、LarsTech 和 JohnnyBoy 向我解释了它的CheckBox工作原理以及我需要查看的内容。

我发现CheckBox它没有公共亮点功能,所以我必须要有创意。
我所做的是我创建了一个自定义CheckBox并且......可能只是向您展示代码:P

public class MyCbx : CheckBox {
    protected override void OnGotFocus(EventArgs e) {
        base.OnGotFocus(e);
        base.OnEnter(e);
        base.OnMouseEnter(e);
    }
    protected override void OnLostFocus(EventArgs e) {
        base.OnLostFocus(e);
        base.OnLeave(e);
        base.OnMouseLeave(e);
    }
    protected override void OnMouseLeave(EventArgs e) {
        if(!this.Focused) {//prevent it from losing highligh if control is in focus
            base.OnMouseLeave(e);
        }
    }
}

因此,当它获得或失去焦点时,我调用 MouseEnter 和 Leave 事件,这将使其变为突出显示状态。

于 2016-01-20T01:10:11.797 回答