将焦点设置在离开组合框时变得可见的下一个项目上

问题描述 投票:0回答:1

我有一个

ComboBox
,其中
AutoCompleteMode
设置为
Suggest
。按顺序的下一个控件是
TextBox
。根据
ComboBox
的所选项目,
TextBox
visible
或不是
visible
。此外,不应输入未包含在
ComboBox
列表元素中的其他文本。因此,在离开
ComboBox
时,我检查
Validating
处的值。

private void cb_Validating(object sender, CancelEventArgs e)
{
  if(cb.SelectedValue == null)
  {
    e.Cancel = true;
  }
  tb.Visible = cb.Text.Contains("XX"); // makes TextBox visible/invisible
}

现在,如果

TextBox
不可见,并且用户在
ComboBox
中键入一些字母,然后按 键转到使
TextBox
可见的项目,然后按制表键,则焦点会跳过以下内容文本框变得可见。这种情况下如何实现文本框不被跳过呢?

c# .net combobox textbox focus
1个回答
0
投票

实现 Validated 事件以显示并选择

TextBox
(如果满足条件)。否则,隐藏它并选择选项卡顺序中的下一个控件。

private void cb_Validating(object sender, CancelEventArgs e)
{
    e.Cancel = cb.SelectedItem == null;            
}

private void cb_Validated(object sender, EventArgs e)
{
    tb.Visible = cb.Text.Contains("some text");
    if (tb.Visible) tb.Select();
    else SelectNextControl(cb, true, true, true, true);
}
© www.soinside.com 2019 - 2024. All rights reserved.