繁体   English   中英

WinForm控件导航

[英]WinForm Controls Navigation

我想为用户提供通过键盘在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;
}

}

如果您确实想在不按Tab键排序的情况下执行此操作,则可以使用此命令

  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。 将TabStop属性设置为false的控件仍然保持其按Tab键顺序的位置,即使使用TAB键在控件之间循环时也会跳过该控件。

表单没有导航问题。 该窗体只有两个TabStop项-RadioButton组和Button。 由于Button的TabStop属性设置为false,因此实际上只有一个TabStop项-RadioButton组,即按Tab键似乎无效。

如果您向窗体添加一个或多个其他控件并尝试浏览该窗体,则此解释应该是很有意义的。

可以使用向上和向下箭头遍历RadioButton组。 它使用TabOrder属性来确定单选按钮的导航顺序。 考虑到表格内容,“ TabOrder”可能有点用词不当。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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