C# ComboBox 不会失去焦点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1219395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ComboBox not losing focus
提问by tomzx
I've been having problems with the ComboBox control. I'm no expert in GUI, but I know this problem is related to the control's focus.
我一直在使用 ComboBox 控件时遇到问题。我不是 GUI 方面的专家,但我知道这个问题与控件的焦点有关。
For some reason, the ComboBox does not lose its focus when I click outside of it. Say for example:
出于某种原因,当我在 ComboBox 外部单击时,它不会失去焦点。比如说:
- I click on the ComboBox to list its items.
- I select one item. This closes the ComboBox.
- I click on the second ComboBox, the first one stays focused.
- 我单击 ComboBox 以列出其项目。
- 我选择一项。这将关闭 ComboBox。
- 我点击第二个 ComboBox,第一个保持专注。
OR
或者
- Click on a ComboBox (contains Point, Solid and Wireframe).
- Click on the form. Press either P, S or W. See the selection get changed.
- 单击组合框(包含点、实体和线框)。
- 点击表格。按 P、S 或 W。查看选择已更改。
Note that the ComboBox only has the DropDownStyleset to ComboBoxStyle.DropDownList. This means that it's the default ComboBox behavior. I thought that the default behavior was that the ComboBox would lose its focus when you clicked out of it, or on another control (button or ComboBox). It is not the case here, why?
请注意, ComboBox 仅将DropDownStyle设置为ComboBoxStyle.DropDownList。这意味着它是默认的 ComboBox 行为。我认为默认行为是当您单击组合框或其他控件(按钮或组合框)时,组合框会失去焦点。这里不是这样,为什么?
UPDATE:What I need is some sort of ActiveComponent = null. The behavior should be similar to the one of Visual Studio were you select Debug or Release (ComboBox) in the standard toolbar. Currently, if I click outside of the ComboBox, it is still focused.
更新:我需要的是某种 ActiveComponent = null。如果您在标准工具栏中选择调试或发布 (ComboBox),则行为应该类似于 Visual Studio 的行为。目前,如果我在 ComboBox 外部单击,它仍然是焦点。
回答by Dave Markle
So what exactly are you saying? Are you saying that your _LostFocus() event handler is not being called? If so, the first place I would look is in your designer-generated event handler mapping code. Sometimes that has a way of being disassociated by doing certain things in the designer (it's rare these days, though...)
那么你到底在说什么?你是说你的 _LostFocus() 事件处理程序没有被调用?如果是这样,我首先要看的是您的设计器生成的事件处理程序映射代码。有时,通过在设计器中执行某些操作可以使这种方式分离(但现在很少见了……)
回答by novacara
Are you sure the problem isn't because neither your frame or your other combobox have a way to gain focus?
您确定问题不是因为您的框架或其他组合框都无法获得焦点吗?
回答by Brandon
You may want to take a look at This topic. Try setting CausesValidation to false on the combo box, see if you can leave it. If an exception is thrown in the OnValidating event handler, it won't deselect the box.
您可能想看看这个主题。尝试将组合框上的 CausesValidation 设置为 false,看看是否可以保留它。如果在 OnValidating 事件处理程序中抛出异常,则不会取消选择该框。
回答by Rob
I experienced a similar problem, but the control was recursively losing and regaining focus; the LostFocus
event handler was being called, but the control immediately regained focus. Setting the CausesValidation
property to false
had no effect.
我也遇到过类似的问题,但是控制递归地失去和重新获得焦点;该LostFocus
事件处理程序被调用,但控制立即恢复焦点。将该CausesValidation
属性设置为false
无效。
In my case, I had bound to the SelectedValue
property instead of the Text
property when binding to a custom object. Because I manually specified the ComboBox item collection and did not provide a data source, the ValueMember
property was missing or invalid (so of course the SelectedValue
property was no use.)
就我而言,在绑定到自定义对象时,我绑定到了SelectedValue
属性而不是Text
属性。因为我手动指定了 ComboBox 项集合,并没有提供数据源,所以ValueMember
属性丢失或无效(所以当然SelectedValue
属性没有用。)
Changing my binding to use the Text
property solved the issue.
更改我的绑定以使用该Text
属性解决了这个问题。
回答by Rob
I had the similar problem and tried all the method you guys suggested. Unfortunately, none of them works. Here is my "simple" solution: send a "ESC" key stoke after you change the SelectedIndex.
我遇到了类似的问题,并尝试了你们建议的所有方法。不幸的是,它们都不起作用。这是我的“简单”解决方案:在更改 SelectedIndex 后发送“ESC”键。
ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
# do whatever you were doing
...
...
SendKeys.Send("{ESC}");
}
It worked for me.
它对我有用。
回答by Anthony
In ***form.Designer.vb
you have some code for each combobox like:
在***form.Designer.vb
你有像每个组合框一些代码:
'OrgDetailsIDComboBox
'
Me.OrgDetailsIDComboBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.MedicoLegalBindingSource, "OrgDetailsID", True))
Me.OrgDetailsIDComboBox.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.MedicoLegalBindingSource, "OrgDetailsID", True))
Me.OrgDetailsIDComboBox.DataSource = Me.OrgBindingSource
Me.OrgDetailsIDComboBox.DisplayMember = "Place"
I fixed the problem by commenting out the first line of code (includes string Forms.Binding("Text", )
. So it seems only the statement for SelectedValue
is required.
我通过注释掉第一行代码(包括 string Forms.Binding("Text", )
。所以似乎只SelectedValue
需要for 语句来解决这个问题。
回答by ADI
Try Leave event instead of LostFocus.
Try Enter event instead of GotFocus.
尝试离开事件而不是 LostFocus。
尝试输入事件而不是 GotFocus。
回答by Rajesh
All you have to do is:
您所要做的就是:
- go to property window of Combobox
- and set Allow Drop="true"
- 转到组合框的属性窗口
- 并设置Allow Drop="true"
The property is intended for some other purpose but it works for this scenario too.
该属性用于其他目的,但它也适用于这种情况。
回答by user8588
The dictionary that combobox take the values has, type index, type value, the type index have to be the same type in your class properity bindingded on combobox. If the types was diferent the combobox never will lose focus.
组合框取值的字典具有,类型索引,类型值,类型索引必须与绑定在组合框上的类属性中的类型相同。如果类型不同,组合框永远不会失去焦点。
回答by Michael Korin
I know this has been a while for this post, but maybe it would help someone in the future who comes across the same problem. I struggled for few days with this, but finally figures it out.
我知道这篇文章已经有一段时间了,但也许它会帮助将来遇到同样问题的人。我为此挣扎了几天,但终于弄清楚了。
if you set CauseViolation to false, then you are not solving the problem and databinding stops working.
如果您将 CauseViolation 设置为 false,那么您就没有解决问题并且数据绑定停止工作。
When you mind SelectedItem to the property like so
当您像这样将 SelectedItem 放在属性中时
combobox.DataBindings.Add("SelectedItem", someObject, "MySelectedItemProperty", false, DataSourceUpdateMode.OnPropertyChanged)
combobox calls the Equals method of the object that you use in the list which assigned to your DataSource. In my case, I needed to overwrite Equals method in this object. For whatever stupid reason, combobox calls this method and passes System.DBNull before actually passing the right object type for comparison. This is where violation occurred in my case and causing violation to fail, hence not releasing the cursor from the combobox. Also the weird part was that the program did not stop when Exception was caused in my Equals method.
组合框调用您在分配给数据源的列表中使用的对象的 Equals 方法。就我而言,我需要覆盖此对象中的 Equals 方法。无论出于何种愚蠢的原因,组合框在实际传递正确的对象类型以进行比较之前调用此方法并传递 System.DBNull。这是在我的情况下发生违规并导致违规失败的地方,因此不会从组合框中释放光标。同样奇怪的是,当我的 Equals 方法中引起异常时,程序并没有停止。
Once I added this code
一旦我添加了这段代码
if (obj.GetType() != this.GetType())
return false;
to my Equals method, everything worked fine. Hope it helps someone.
对于我的 Equals 方法,一切正常。希望它可以帮助某人。