user3165438
user3165438

Reputation: 2661

WinForm Controls Navigation

I would like to give user the option to navigate on WinForm controls by keyboard.

I want to navigate on some of the controls, not on all of them.
For example- to navigate between radio buttons and skip on a button which also exist on the same form.

I set the TabStop property of the botton to "False", but when navigating and reaching the index of the button control, the button is not focused since as mentioned I set TabStop to false, but the navigation is waiting at the recent spot and does not continue.

Any ideas how can we avoid it?

Designer code:

    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;
}

}

Upvotes: 0

Views: 2046

Answers (3)

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

According to How to: Set the Tab Order on Windows Forms:

To set the tab order of a control

  1. On the View menu, click Tab Order.

    This activates the tab-order selection mode on the form. A number (representing the TabIndex property) appears in the upper-left corner of each control.

  2. Click the controls sequentially to establish the tab order you want.

To remove a control from the tab order

  1. Set the control's TabStop property to false in the Properties window. A control whose TabStop property has been set to false still maintains its position in the tab order, even though the control is skipped when you cycle through the controls with the TAB key.

Upvotes: 1

StevieB
StevieB

Reputation: 1000

There's no navigation issue with the Form. The Form only has two TabStop items - the RadioButton group and the Button. Since the Button's TabStop property is set to false there is effectively only one TabStop item - the RadioButton group i.e. pressing the Tab key appears to have no effect.

If you add one or more additional controls to the Form and try navigating through it this explanation should make complete sense.

The RadioButton group can be traversed by using the up and down arrows. It uses the TabOrder property to determine the order of navigating the RadioButtons. 'TabOrder' may be a bit of a mis-nomer when considered in the light of the contents of your Form.

Upvotes: 0

csharp newbie
csharp newbie

Reputation: 638

If you really want to do this without tab order, you can use this

  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);
        }

Upvotes: 2

Related Questions