【问题标题】:vb.net How to overwrite the text in a combobox after the dropdownclosed eventvb.net如何在下拉关闭事件后覆盖组合框中的文本
【发布时间】:2018-06-06 08:03:07
【问题描述】:

我创建了这段代码来说明这个想法,它使用了一个组合框和一个文本框

Private Sub ComboBox2_DropDown(sender As Object, e As EventArgs) Handles ComboBox2.DropDown
    ComboBox2.Items.Clear()
    ComboBox2.Items.Add("0001 | Apple")
    ComboBox2.Items.Add("0002 | Pear")
    ComboBox2.Items.Add("0003 | Banana")
    ComboBox2.Items.Add("0004 | Pineapple")
    ComboBox2.Items.Add("0005 | Cherry")
End Sub

Private Sub ComboBox2_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox2.DropDownClosed
    Dim selecteditem As String = ComboBox2.Items(ComboBox2.SelectedIndex)
    ComboBox2.Text = Strings.Left(selecteditem,4)
    TextBox2.Text = Strings.Left(selecteditem,4)
End Sub

当我从组合框中选择一个项目时,会发生组合框一直显示整个字符串,而文本框只显示前 4 个字符。

关闭组合框后如何覆盖组合框文本?

* 编辑 * 我尝试了这些解决方案的组合,但遇到了问题,因为数据已绑定到数据源,因此无法更改项目。 这是新代码:

Private Sub ComboBox2_DropDown(sender As Object, e As EventArgs) Handles ComboBox2.DropDown
    SQL.ExecQuery($"select ID, Name, RTRIM(ID + ' | ' + Name) as SingleColumn from GCCTEST.dbo.tblFruit")
    ComboBox2.DataSource = SQL.DBDT
    ComboBox2.DisplayMember = "SingleColumn"
End Sub
Private Sub ComboBox2_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox2.DropDownClosed
    ComboBox2.DisplayMember = "ID"
    ComboBox2.SelectedIndex = 0
End Sub

现在我只需要将 0 作为我选择的索引...

【问题讨论】:

    标签: vb.net combobox overwrite


    【解决方案1】:

    以下应该有效。 如果没有必要,不要在每个下拉列表中填充组合框,而是在加载Form 时调用FillComboBox-方法。

    Private Sub FillComboBox()
        SQL.ExecQuery($"select ID, Name, RTRIM(ID + ' | ' + Name) as SingleColumn from GCCTEST.dbo.tblFruit")
        ComboBox2.DataSource = SQL.DBDT
        ComboBox2.DisplayMember = "ID" 
        ComboBox2.ValueMember = "ID"
    End Sub
    
    Private Sub ComboBox2_DropDown(sender As Object, e As EventArgs) Handles ComboBox2.DropDown
        Me.ComboBox2.DisplayMember = "SingleColumn"
    End Sub
    
    Private Sub ComboBox2_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox2.SelectionChangeCommitted
        Dim selV As Object = Me.ComboBox2.SelectedValue
    
        Me.TextBox2.Text = CStr(selV)
        Me.ComboBox2.DisplayMember = "ID"
    
        'Set the current value again, otherwise the combobox will always display the first item
        Me.ComboBox2.SelectedValue = selV
    End Sub
    

    【讨论】:

    • 这也是我真正喜欢的可行解决方案。它更符合我已经拥有的。谢谢。
    • 顺便说一下,我将 FillComboBox 保留在表单加载事件之外。在这种情况下,这不是问题,但组合框中有几个组合框和大量数据,加载表单需要额外的几秒钟。
    【解决方案2】:

    我使用了一些属性和 .net String.SubString 方法,而不是旧的 vb6 Strings.Left

    Private Sub ComboBox1_DropDownClosed(sender As Object, e As EventArgs) Handles ComboBox1.DropDownClosed
            Dim SelectedString As String = ComboBox1.SelectedItem.ToString
            Dim ChangedString As String = SelectedString.Substring(0, 4)
            Dim index As Integer = ComboBox1.SelectedIndex
            ComboBox1.Items(index) = ChangedString
    End Sub
    

    您可以一一填写您的组合框,以避免出现以下绑定问题...

    Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown
            Using cn As New SqlConnection("Your connection string")
                Using cmd As New SqlCommand("Select ID, Name From tblFruit;", cn)
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader
                        ComboBox1.BeginUpdate()
                        While dr.Read
                            ComboBox1.Items.Add(dr(0).ToString & " | " & dr(1).ToString)
                        End While
                        ComboBox1.EndUpdate()
                    End Using
                End Using
        End Using
    

    【讨论】:

    • 这适用于给定的示例,但实际上组合框绑定到数据源,这会产生如下问题:设置 DataSource 属性时无法修改 Items 集合
    • @MiguelTerlaak 您可以创建一个填充组合框的 sub 而不是绑定数据源。不要忘记在 sub 的开头和结尾使用 SuspendLayout 和 ResumeLayout 以提高性能。
    • 我正在使用 sql 查询填充组合框,据我所知,该查询会自动绑定数据源。我在问题的底部添加了那段新代码。
    • @MiguelTerlaak 我添加了代码来演示在没有绑定的情况下填充组合框。正如@David Wilson 所建议的,我使用了.BeginUpdate.EndUpdate
    • @Mary 谢谢,这是一个可行的解决方案。只需要添加一个 combobox1.Items.Clear() 否则组合框会不断填满并检查 combobox1.selectedindex > -1 以防用户在第一个实例中没有选择任何内容。
    【解决方案3】:

    您可以解决这个图形问题,将Label 放入您的Form 并将其移动到您的ComboBox 上。

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
        Me.Label1.AutoSize = False
        Me.Label1.BackColor = Me.ComboBox1.BackColor
        Me.Label1.Location = New Point(Me.ComboBox1.Location.X + 1, Me.ComboBox1.Location.Y + 1)
        Me.Label1.Size = New Size(Me.ComboBox1.Width - 18, Me.ComboBox1.Height - 2)
    
        Me.ComboBox1.Items.Add("0001 | Apple")
        Me.ComboBox1.Items.Add("0002 | Pear")
        Me.ComboBox1.Items.Add("0003 | Banana")
        Me.ComboBox1.Items.Add("0004 | Pineapple")
        Me.ComboBox1.Items.Add("0005 | Cherry")
    
    End Sub
    
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    
        Me.Label1.Text = Trim(Me.ComboBox1.SelectedItem.Split("|")(0))
    
    End Sub
    

    请注意:

    • 您可以在 Form 加载事件期间填充您的 ComboBox 一次
    • 您可以使用SelectedIndexChanged 代替DropDownDropDownClosed 事件
    • 如果这不是图形问题,请查看DisplayMemberValueMember 属性

    【讨论】:

      猜你喜欢
      • 2016-10-11
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      相关资源
      最近更新 更多