Winform控制导航



我想为用户提供键盘上的Winform控件的选项。

我想在某些控件上导航,而不是所有控件。
例如 - 在无线电按钮之间导航并跳过一个也存在于同一表单上的按钮。

我将Botton的TabStop属性设置为" false",但是当导航和达到按钮控件的索引时,该按钮并不集中,因为如前所述,我将TabStop设置为False,但是导航正在最近等待斑点,不会继续。

有什么想法我们如何避免它?

设计师代码:

    partial class Form1
{
    /// <summary>
    /// Designer variable used to keep track of non-visual components.
    /// </summary>
    private System.ComponentModel.IContainer components = null;
    /// <summary>
    /// Disposes resources used by the form.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing) {
            if (components != null) {
                components.Dispose();
            }
        }
        base.Dispose(disposing);
    }
    /// <summary>
    /// This method is required for Windows Forms designer support.
    /// Do not change the method contents inside the source code editor. The Forms designer might
    /// not be able to load this method if it was changed manually.
    /// </summary>
    private void InitializeComponent()
    {
        this.radioButton1 = new System.Windows.Forms.RadioButton();
        this.radioButton2 = new System.Windows.Forms.RadioButton();
        this.radioButton3 = new System.Windows.Forms.RadioButton();
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // radioButton1
        // 
        this.radioButton1.Location = new System.Drawing.Point(62, 62);
        this.radioButton1.Name = "radioButton1";
        this.radioButton1.Size = new System.Drawing.Size(104, 24);
        this.radioButton1.TabIndex = 1;
        this.radioButton1.TabStop = true;
        this.radioButton1.Text = "radioButton1";
        this.radioButton1.UseVisualStyleBackColor = true;
        // 
        // radioButton2
        // 
        this.radioButton2.Location = new System.Drawing.Point(62, 92);
        this.radioButton2.Name = "radioButton2";
        this.radioButton2.Size = new System.Drawing.Size(104, 24);
        this.radioButton2.TabIndex = 2;
        this.radioButton2.TabStop = true;
        this.radioButton2.Text = "radioButton2";
        this.radioButton2.UseVisualStyleBackColor = true;
        // 
        // radioButton3
        // 
        this.radioButton3.Location = new System.Drawing.Point(62, 122);
        this.radioButton3.Name = "radioButton3";
        this.radioButton3.Size = new System.Drawing.Size(104, 24);
        this.radioButton3.TabIndex = 3;
        this.radioButton3.TabStop = true;
        this.radioButton3.Text = "radioButton3";
        this.radioButton3.UseVisualStyleBackColor = true;
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(62, 201);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(159, 38);
        this.button1.TabIndex = 0;
        this.button1.TabStop = false;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.radioButton3);
        this.Controls.Add(this.radioButton2);
        this.Controls.Add(this.radioButton1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.RadioButton radioButton3;
    private System.Windows.Forms.RadioButton radioButton2;
    private System.Windows.Forms.RadioButton radioButton1;
}

}

如果您真的想在不订单的情况下执行此操作,则可以使用此

  int gTabCounter = 0;     
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData.Equals(Keys.ShiftKey | Keys.Shift)) //you can set any key you want
            {
                List<Control> controls = new List<Control>();
                controls.Add(button1);
                controls.Add(textBox1);                
                if (gTabCounter > 1) gTabCounter = 0;
                controls[gTabCounter].Focus();
                gTabCounter++;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

根据如何:在Windows表单上设置选项卡顺序:

设置控件的选项卡顺序

  1. 在视图菜单上,单击订单。

    这将激活表单上的选项卡选择模式。一个号码 (代表Tabindex属性)出现在左上方 每个控件的角落。

  2. 依次单击控件以建立所需的选项卡顺序。

从选项卡顺序删除控件

  1. 将控件的TabStop属性设置为false在属性中 窗户。 将其塔塔普属性设置为false的控件 即使控件是 当您使用选项卡键循环到控件时跳过。

表格没有导航问题。该表格只有两个TabStop项目 - RadioButton组和按钮。由于按钮的TabStop属性设置为false,实际上只有一个TabStop项目 - RadioButton组,即按下Tab键似乎没有效果。

如果您向表单中添加一个或多个其他控件并尝试通过它导航,则该解释应该完全有意义。

可以使用向上和向下箭头来遍历RadioButton组。它使用访问权限属性来确定导航RadioButtons的顺序。根据您的形式的内容,考虑到"守护者"可能有点麻烦。

最新更新