我想让用户通过键盘选择导航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;
}
}
答案 0 :(得分:2)
如果您确实想要在没有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);
}
答案 1 :(得分:1)
根据How to: Set the Tab Order on Windows Forms:
设置控件的Tab键顺序
在“视图”菜单上,单击“Tab Order”。
这将激活表单上的选项卡顺序选择模式。一个号码 (表示TabIndex属性)出现在左上角 每个控件的一角。
- 按顺序单击控件以建立所需的Tab键顺序。
醇>从Tab键顺序中删除控件
- 在“属性”中将控件的TabStop属性设置为false 窗口。 其TabStop属性仍设置为false的控件 即使控件是,也保持其在Tab键顺序中的位置 使用TAB键循环控件时跳过。
醇>
答案 2 :(得分:0)
表单没有导航问题。 Form只有两个TabStop项 - RadioButton组和Button。由于Button的TabStop属性设置为false,实际上只有一个TabStop项 - RadioButton组,即按Tab键似乎没有效果。
如果您向表单添加一个或多个其他控件并尝试浏览它,则此解释应该完全正确。
可以使用向上和向下箭头遍历RadioButton组。它使用TabOrder属性来确定导航RadioButtons的顺序。根据表格内容考虑,“TabOrder”可能有点误。