vb.net当绑定到Objectdatasource时,Combobox重置为在失去焦点时未选中

时间:2013-07-24 15:11:12

标签: vb.net combobox lostfocus

抱歉,我是vb.net 2010的新手。我也是子类化的新手。 comobobox用作查找。

我有一个绑定到bindingSource的comboxbox。问题是在我选择了我的组合框上的一个项目并失去其焦点后它恢复为空白。为什么是这样?怪异。

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

2 个答案:

答案 0 :(得分:0)

尝试将其原因验证值设为False。如果那不起作用,可能你的ComboBox DropDownStyle没有设置为DropDownList

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

CausesValidation="false"

或尝试在代码中添加此项以用于dropdownstyle

ComboBox.DropDownStyle = ComboBoxStyle.DropDownList 

答案 1 :(得分:0)

我想我弄明白了这个问题。在我的User.vb类中,我有一个公共属性“UserType”,在我的UserType.vb类中,我也有一个名为“UserType”的公共属性。这会导致冲突吗?

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

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