UI 问题 - 控制控件状态和焦点
在开发 .NET Windows 窗体应用程序时,我陷入了一些僵局。问题与控件之间的焦点切换以及它如何维护表单控件的状态有关。
UI 中有三个主要控件 - 两个组合框和一个按钮。
- 两个组合框均以
SelectedItem = null
开头。 - 两个组合框都有一组明确的有效选项可供选择。
- 两个组合框都使用
AutoCompleteMode.SuggestAppend
,并将AutoCompleteSource
设置为AutoCompleteSource.ListItems
。这意味着用户可以在组合框中键入内容,并弹出建议以供选择值。 - 组合框 #1 始终启用。
- 除非在 ComboBox #1 中选择了特定值,否则 ComboBox #2 将被禁用。
- 该按钮仅在以下情况下启用:
- 已从始终启用的组合框中选择了有效值。
- 已从第二个组合框中选择了有效值IFF已在第一个组合框中选择了特定值。否则,它的值将被忽略。
- 在所有其他情况下,它都会被禁用。
问题在于键盘导航和控件焦点,以及它们如何与维护各种控件的启用状态相关的逻辑一起发挥作用。
简而言之,当为组合框选择一个值时,按 TAB 键(对组合框进行选择)会将焦点转移到窗体上的下一个控件(按照 TabIndex 顺序),然后启用/禁用控件的逻辑是跑步。
启用/禁用控件的逻辑发生在关联组合框的 Validating/Validated 事件中。
最终结果是控件在获得焦点后可以被禁用。
例如:
- 仅启用组合框#1。组合框 #2 和按钮被禁用。所有 ComboBox 的 SelectedItem 都设置为 null。
- 将焦点设置到组合框 #1。
- 在框中键入有效选项的前几个字母。
- 按 Tab 键。
当我按 TAB 时,似乎在更改 SelectedItem 之前焦点已转移到下一个控件。在 SelectedItem 更改之前,无法运行启用或禁用其他 UI 控件的业务逻辑。
理想情况下,我想要的是相反的方式:焦点应该在 UI 控件的 SelectedItem 更新后转移,从而更新控件状态的逻辑已经运行。
有人对我如何做到这一点有任何建议吗?
I've reached a bit of an impasse when developing a .NET windows forms app. The problem has to do with switching focus between controls, and how it plays with maintaining the form's control's states.
There are three main controls in the UI – two combo boxes and a button.
- Both combo boxes start off with
SelectedItem = null
. - Both combo boxes have an explicit set of valid options that can be selected.
- Both combo boxes are using
AutoCompleteMode.SuggestAppend
, with theAutoCompleteSource
set toAutoCompleteSource.ListItems
. This means users can type in to the combo boxes and have suggestions pop up for the value to select. - ComboBox #1 is always enabled.
- ComboBox #2 is disabled unless a specific value has been selected in ComboBox #1.
- The button is enabled only when:
- A valid value has been selected from the always-enabled combo box.
- A valid value has been selected from the second combo box IFF a specific value has been selected in the first combo box. Otherwise, its value is ignored.
- In all other cases it is disabled.
The problem is to do with keyboard navigation and control focus, and how they play together with the logic associated with maintaining the Enabled
states of the various controls.
In a nutshell, when selecting a value for a combo box, pressing TAB (to make your selection for the combo box) gives focus to the next control on the form (as per the TabIndex order) BEFORE the logic to enable/disable controls is run.
The logic to enable/disable the controls is happening in the Validating/Validated events of the associated combo boxes.
The net result is that a control can be disabled AFTER it has been given focus.
For example:
- Only ComboBox #1 is enabled. ComboBox #2 and the Button are disabled. All ComboBox's SelectedItem are set to null.
- Set focus to ComboBox #1.
- Type the first few letters of a valid option for the box.
- Press TAB.
When I press TAB, it seems that focus is given to the next control before the SelectedItem is changed. The business logic to enable or disable the other UI controls can't be run until SelectedItem is changed.
Ideally, what I want is the other way around: focus should be shifted AFTER the UI control’s SelectedItem has been updated, thus the logic to update the control's states has been run.
Does anybody have any suggestions as to how I could go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否在
SelectedIndexChanged
/SelectedValueChanged
中显式更改适当组合框的焦点?您可能想要检查焦点当前所在的位置,并且仅在焦点位于禁用的控件上时才进行移动。Can you explicitly change focus in
SelectedIndexChanged
/SelectedValueChanged
for the appropriate combo box? You might want to check where focus is currently, and only shift if it's on the disabled control.