【问题标题】:vb.net Combobox resets to un-selected on lost focus when bound to Objectdatasource绑定到 Objectdatasource 时,vb.net Combobox 在失去焦点时重置为未选中
【发布时间】:2013-07-24 15:11:11
【问题描述】:

对不起,我是 vb.net 2010 的新手。我也是子类化的新手。组合框用作查找。

我有一个绑定到 bindingSource 的组合框。问题是在我在组合框中选择一个项目并失去焦点后,它会恢复为空白。为什么是这样?很奇怪。

 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(例如“编码器”)中选择一个项目时,当它失去焦点时,选择将恢复为未选择状态。 :-(

【问题讨论】:

  • Weird 我试过 BindingSource.suspendBinding 和 BindingSource.ResumeBinding 似乎可以解决问题。但编译后问题再次存在。 :-(

标签: vb.net combobox lostfocus


【解决方案1】:

尝试将原因验证值设为 False。如果这不起作用,可能您的 ComboBox DropDownStyle 未设置为 DropDownList

在前面的代码中将以下属性添加到您的组合框中,然后尝试

CausesValidation="false"

或尝试在下拉样式的代码中添加它

ComboBox.DropDownStyle = ComboBoxStyle.DropDownList 

【讨论】:

  • 我该怎么做?抱歉,我是 vb.net 的新手。
  • 我试过原因validation = false。它确实解决了组合框失去焦点的空白文本属性,但它也有其他效果。结果是没有数据保存在数据库中。 :-(
  • 我试过简单,下拉,下拉列表问题仍然存在。组合框在失去焦点时恢复为空字符串。呜呜呜……
【解决方案2】:

我想我找到了问题所在。在我的 User.vb 类中,我有一个公共属性“UserType”,在我的 UserType.vb 类中,我也有一个名为“UserType”的相同公共属性。这会引起冲突吗?

我删除了两个课程并重新开始。在我的 UserType.vb 类中,我将公共属性重命名为 Utype。之后,我在添加/编辑表单上重新配置了数据绑定。组合框问题消失了。

重复的公共属性名称是组合框失去焦点问题的原因吗?我仍然不确定这是否是真正的问题。但我很高兴我的组合框现在可以正常工作。

【讨论】:

  • 我的错误问题仍然存在。导致问题的代码是表单加载时的 UserBindingSource.Add(newUser)。 :-(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多