当绑定到Objectdatasource时,vb.net组合框在失去焦点时重置为未选择
对不起,我是vb.net 2010的新手。我还不熟悉子类化。ComboBox用作查找 我有一个绑定到bindingSource的comboxbox。问题是,当我在组合框中选择一个项目并失去焦点后,它会恢复为空白。为什么会这样?奇怪当绑定到Objectdatasource时,vb.net组合框在失去焦点时重置为未选择,vb.net,combobox,lostfocus,Vb.net,Combobox,Lostfocus,对不起,我是vb.net 2010的新手。我还不熟悉子类化。ComboBox用作查找 我有一个绑定到bindingSource的comboxbox。问题是,当我在组合框中选择一个项目并失去焦点后,它会恢复为空白。为什么会这样?奇怪 My tables: tblUsers -FullName -UserName -Password -UserTypeID tblUserType -UserTypeID -UserType (Admin, Supervisor,
My tables:
tblUsers
-FullName
-UserName
-Password
-UserTypeID
tblUserType
-UserTypeID
-UserType (Admin, Supervisor, Encoder)
My Class Library:
-Users.vb = table encapsulation of tblUsers
-UserDB.vb = methods for my tblUsers
-UserType.vb = table encapsulation of tblUserType
-UserTypeDB.vb = methods for my tblUserType
Data Sources:
-UserBindingSource = Users.vb Class
-UserTypeBindingSource = UserType.vb Class
Databindings for my UserTypeComboBox:
UserTypeComboBox.DataSource = UserTypeBindingSource
UserTypeComboBox.DisplayMember = UserType
UserTypeComboBox.ValueMember = UserTypeID
UserTypeComboBox.SelectedValue = UsersBindingSource - UserTypeID
My Code Below:
Public Class frmUsers_AddEdit
Private newUser As Users
Private usertypeList As List(Of UserType)
Private Sub LoadComboboxes()
Try
usertypeList = UserTypeDB.GetUserTypeList
UserTypeComboBox.DataSource = usertypeList
Catch ex As Exception
MessageBox.Show(ex.Message, ex.GetType.ToString)
End Try
End Sub
Private Sub frmUsers_AddEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.LoadComboboxes()
UsersBindingSource.Add(newUser)
End Sub
end class
frmUsers_AddEdit是我用于向数据库中添加/编辑用户数据的表单。我还在添加阶段。还没有用于编辑的代码
组合框问题:每次我从UserTypeCombobox(例如“编码器”)中选择一个项目时,当它失去焦点时,选择将恢复为未选择-(尝试将原因验证值设为False。如果不起作用,可能您的ComboBox DropDownStyle未设置为DropDownList 将以下属性添加到前面代码中的组合框中,然后重试
CausesValidation="false"
或者尝试将其添加到dropdownstyle的代码中
ComboBox.DropDownStyle = ComboBoxStyle.DropDownList
我想我已经解决了这个问题。在我的User.vb类中,我有一个公共属性“UserType”,在我的UserType.vb类中,我也有相同的公共属性“UserType”。这会导致冲突吗 我删除了这两个类并重新开始。在我的UserType.vb类中,我将公共属性重命名为Utype。之后,我重新配置了添加/编辑表单上的数据绑定。组合框问题消失了
重复的公共属性名称是否是combobox失去焦点问题的原因?我仍然不确定这是否是真正的问题。但我很高兴我的combobox现在工作正常。奇怪,我尝试了BindingSource.suspendBinding和BindingSource.ResumeBinding,似乎解决了问题。但编译后问题再次出现。:-(我该怎么做?对不起,我是vb.net新手。我尝试了causesvalidation=false。它确实解决了组合框的“失去焦点”空白文本属性,但它还有其他效果。结果是数据库中没有保存任何数据。:-(我尝试了简单、下拉、下拉列表问题仍然存在。Combobox在失去焦点时恢复为空字符串。Grrrr…我的错误问题仍然存在。导致问题的代码是UserBindingSource。在表单加载时添加(newUser):-(