我向您展示了我的代码,通过该代码我将光标焦点发送到下一个控件,但我想通过按shift + tab键将焦点设置在上一个控件上。
protected override bool ProcessTabKey(bool forward)
{
Control ctl = this.ActiveControl;
if (ctl != null && ctl is TextBox)
{
TextEdit tb = (TextEdit)ctl.Parent;
if (SelectTextBoxes.Contains(tb) && tb.Text.Length == 0)
{
return true;
}
}
return base.ProcessTabKey(forward); // process TAB key as normal
}
答案 0 :(得分:0)
尝试下一步。打开KeyPreview
,以便表单可以注册所有按键。输入将TabStop
打开到单个列表的所有控件,并按选项卡索引对该列表进行排序。覆盖OnKeyDown
以检查是否按下了 shift键,如果仅按 tab ,则在ProcessTabKey
方法内部向前移动,如果 shift + tab 被按下了。
private List<Control> _tabControls = new List<Control>();
public MyForm()
{
InitializeComponent();
this.KeyPreview = true;
TabControlsToList(this.Controls);
_tabControls = _tabControls.OrderBy(x => x.TabIndex).ToList();
}
private void TabControlsToList(Control.ControlCollection controls)
{
foreach (Control control in controls)
{
if (control.TabStop == true)
_tabControls.Add(control);
if (control.HasChildren)
TabControlsToList(control.Controls);
}
}
protected override void OnKeyDown(KeyEventArgs args)
{
if ((args.Modifiers == Keys.Shift) && (args.KeyCode == Keys.Tab))
interceptTabKey = !interceptTabKey;
base.OnKeyDown(args);
}
private bool interceptTabKey = true;
protected override bool ProcessTabKey(bool forward)
{
// We can intercept/process the [Keys.Tab] via this method.
if (interceptTabKey)
{
if (forward) // [Keys.Shift] was not used
{
this.SelectNextControl(this.ActiveControl, true, true, true, true);
}
else // [Keys.Shift] was used
{
int currentIndex = _tabControls.IndexOf(this.ActiveControl);
var control = _tabControls[currentIndex == 0 ? _tabControls.Count - 1 : currentIndex - 1];
control.Select();
}
// [return true;] -- To indicate that a control is selected.
return true;
}
// Do this normally when not intercepted
return base.ProcessTabKey(forward);
}
答案 1 :(得分:-1)
下面的代码在我的项目中运行正常。感谢Smola,因为我修改了他的代码以实现我的目标。
private List<TextEdit> SelectTextBoxes = new List<TextEdit>();
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
SelectTextBoxes = new List<TextEdit>()
{
textEdit1,textEdit3, textEdit4, textEdit2
};
SelectTextBoxes = SelectTextBoxes.OrderBy(x => x.TabIndex).ToList();
}
protected override void OnKeyDown(KeyEventArgs args)
{
if ((args.Modifiers == Keys.Shift) && (args.KeyCode == Keys.Tab))
{
interceptTabKey = !interceptTabKey;
}
base.OnKeyDown(args);
}
private bool interceptTabKey = true;
protected override bool ProcessTabKey(bool forward)
{
TextEdit tb = (TextEdit)this.ActiveControl.Parent;
// We can intercept/process the [Keys.Tab] via this method.
if (interceptTabKey)
{
if (forward) // [Keys.Shift] was not used
{
if (SelectTextBoxes.Contains(tb) && tb.Text.Length > 0)
this.SelectNextControl(this.ActiveControl, true, true, true, true);
}
else // [Keys.Shift] was used
{
int currentIndex = SelectTextBoxes.IndexOf(tb);
var control = SelectTextBoxes[currentIndex == 0 ? SelectTextBoxes.Count - 1 : currentIndex - 1];
control.Select();
}
// [return true;] -- To indicate that a control is selected.
return true;
}
// Do this normally when not intercepted
return base.ProcessTabKey(forward);
}