Combobox选择的项目与其选中的项目不匹配

时间:2013-05-15 09:03:02

标签: vb.net winforms events combobox

我遇到的问题是,在字母结束后,空格会出现超过Combobox.SelectedItem的长度。为什么?我该如何解决这个问题?

下面以视觉方式显示我的问题是一个非常短小的视频。 Here is a small video

Imports System.Data.SqlClient
Public Class Main
WithEvents bsData As New BindingSource
Dim sConn As New SqlConnection
Dim dt As New DataTable
Dim ds As New DataSet
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    sConn.ConnectionString = "Data Source=PC-N39;Initial Catalog=Esi01;Persist    Security Info=True;User ID=sa;Password=sas"
    sConn.Open()
    Try
        Dim myTable As DataTable = New DataTable("MyTable")
        myTable.Columns.Add(New DataColumn("Group Code"))
        myTable.Columns.Add(New DataColumn("Description"))
        myTable.Columns.Add(New DataColumn("NothingSerious"))
        Dim cmd As New SqlCommand("Select * from tbUnit", sConn)
        Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.Default)
        Dim myRow As DataRow
        While dr.Read()
            myRow = myTable.NewRow
            myRow.Item(0) = dr(0)
            myRow.Item(1) = dr(1)
            myRow.Item(2) = dr(2)
            myTable.Rows.Add(myRow)
        End While
        dr.Close()
        Dim myData4 As DataTable = myTable
        ds = New DataSet()
        ds.Tables.Add(myData4)
        MultiColumnCombo1.DisplayMember = "Group Code"
        MultiColumnCombo1.DrawMode = DrawMode.OwnerDrawVariable
        MultiColumnCombo1.ColumnWidths = "50;150"
        MultiColumnCombo1.DataSource = myData4
        MultiColumnCombo1.Text = String.Empty
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
    sConn.Close()
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

MultiColumnCombo1是什么类型的?我想这是第三方组件。 就我在你的视频中看到的而言,它似乎占据了数据中最大的元素,占用它的长度并为其他元素添加空格以使它们具有相同的大小。

您可能无法更改组件的行为,但您可以将相同的逻辑应用于您在TextBox中输入的内容:添加空格以使其匹配。或者,如果您只是需要制定一个条件来测试它们是否相等,您可以执行以下操作:

if(YourComboboxSelectedItem.Trim() == StringYourAreComparingItTo)

请参阅String.Trim(),这将丢弃额外的空格。