WInforms组合框SelectionChangeCommitted事件并不总是更改SelectedValue [英] WInforms Combobox SelectionChangeCommitted event doesn't always change SelectedValue

查看:38
本文介绍了WInforms组合框SelectionChangeCommitted事件并不总是更改SelectedValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在VS 2010中使用VB.Net构建的WinForms应用,而我在下面的问题上scratch之以鼻.

I have a WinForms app built with VB.Net in VS 2010, and I'm scratching my head over the following issue.

我有一个带有组合框的表单,该表单在加载表单时绑定到数据源:

I have a form with a combobox which gets bound to a datasource when the form is loaded:

 With Me.cboCompany
    .DataBindings.Clear()
    .DataSource = Me.m_dsBidResults.Tables("Company")
    .ValueMember = "company_id"
    .DisplayMember = "company_name"
    .DataBindings.Add("SelectedValue", Me.m_dsBidResults, Company.company_id")
 End With

我正在使用cboCompany.SelectionChangeCommitted事件来按选定的公司ID过滤datagridview:

I'm using the cboCompany.SelectionChangeCommitted event to filter a datagridview by the selected company ID:

Private Sub cboCompany_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCompany.SelectionChangeCommitted
    Dim intCompanyIDN As Integer        
    intCompanyIDN = CInt(cboCompany.SelectedValue)
    SelectBidder(intCompanyIDN)  ' sub to filter datagridview, update labels      
End Sub

这似乎很好用,只要用户不将焦点切换到其他控件,然后再返回到组合框即可.切换焦点后,如果用户随后将组合框选择更改为下拉列表中的第一项(SelectedIndex = 0),则会触发SelectionChangeCommitted事件,但SelectedValue仍设置为先前选择的值.我已经通过在上面的事件处理程序中添加一个消息框并同时显示SelectedIndex和SelectedValue来验证了这一点.

This seems to work fine, as long as the user doesn't switch focus to some other control and then go back to the combobox. After switching focus, if the user then changes the combobox selection to the first item in the dropdown list (SelectedIndex = 0), the SelectionChangeCommitted event fires, but the SelectedValue remains set to the previously selected value. I've verified this by adding a message box in the above event handler, displaying SelectedIndex and SelectedValue side-by-side.

'add this to SelectionChangeCommitted event handler
MsgBox(String.Format("Selected Index: {0}, Selected Value: {1}", cboCompany.SelectedIndex, cboCompany.SelectedValue))

如果用户将SelectedIndex更改为0以外的其他值,则不会发生这种情况;一切都按预期进行.我已验证绑定到的表包含company_id和company_name的唯一值.

This does NOT happen if the user changes SelectedIndex to anything other than 0; everything behaves as expected. I've verified that the table I'm binding to contains unique values for company_id and company_name.

我是否需要使用其他事件来验证SelectedValue确实发生了变化?另外,也可以欢迎提出可靠的解决方法的想法.

Do I need to use some other event to verify that the SelectedValue has actually changed? Alternatively, ideas for a reliable workaround would be welcome.

推荐答案

从表单中删除此行,然后重试

Remove this line from your form and try again

.DataBindings.Add("SelectedValue", Me.m_dsBidResults, Company.company_id")

说明:
此代码告诉combobx,其SelectedValue属性应该绑定到数据集的company_id.这没用,因为您已经通过设置数据源添加了一个列表,并且说了值成员和显示成员是什么.然后,通过使用SelectionChangeCommitted事件来实现自己的逻辑,以解决在值更改时应该执行的操作.删除的多余行仅在您有另一个绑定对象(例如Person类型)时才有用,该对象具有显示他在哪个公司工作的属性.在这种情况下,当组合框更改时,您希望将选择的company_id推送到Person对象.

Explanantion:
This code told the combobx that its SelectedValue-property should be bound to the company_id of the dataset. Which is useless, because you already added a list by setting the datasource and you said what the valuemember and displaymember are. Then you implemented your own logic on what it should do when the value changes by using the SelectionChangeCommitted event. The extra line that you removed is only usefull if you have another bound object, say of type Person which has a property that shows in what company he works. In that scenario, when the combobox changes, you want the select company_id to be pushed to the Person-object. Something like

personBindingsource1.DataSource = somePerson;
cboCompany.DataBindings.Add("SelectedValue", personBindingsource1, "WorksAtCompany")

希望它现在变得更有意义了:)

Hopefully it makes some more sense now :)

这篇关于WInforms组合框SelectionChangeCommitted事件并不总是更改SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆