简体   繁体   English

vb.net如何在dropdownclosed事件后覆盖组合框中的文本

[英]vb.net How to overwrite the text in a combobox after the dropdownclosed event

I created this piece of code to illustrate the idea, it uses a combobox and a textbox 我创建了这段代码来说明这个想法,它使用了一个组合框和一个文本框

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

When I select an item from the combobox what happens is that the combobox keeps showing the whole string while the textbox only shows the first 4 characters. 当我从组合框中选择一个项目时,会发生以下情况:组合框始终显示整个字符串,而文本框仅显示前4个字符。

How can I overwrite the combobox text after I close the combobox? 关闭组合框后,如何覆盖组合框文本?

* edit * I tried a combo of the solutions but ran into a problem because the data was bound to a datasource so it's not possible to change the item. *编辑*我尝试了多种解决方案,但是遇到了问题,因为数据已绑定到数据源,因此无法更改项目。 This is the new code: 这是新代码:

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

Now I only need to have the 0 be the index I chose... 现在我只需要使0成为我选择的索引即可...

I used a few properties and .net String.SubString method instead of the old vb6 Strings.Left . 我使用了一些属性和.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

You can fill your combo box one by one to avoid binding problems as follows... 您可以一个一个地填充组合框,以避免出现以下绑定问题...

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

The following should work. 以下应该工作。 If not necessary, don't populate the combobox on every drop-down, instead call the FillComboBox -method when loading the Form . 如有必要,不要在每个下拉列表中填充组合框,而是在加载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

You can solve this graphical problem putting a Label in your Form and moving it over your ComboBox . 您可以解决此图形问题,将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

Please note that: 请注意:

  • you can populate your ComboBox a single time during Form load event 您可以在Form加载事件中一次填充ComboBox
  • you can use SelectedIndexChanged instead of DropDown and DropDownClosed event 您可以使用SelectedIndexChanged代替DropDownDropDownClosed事件
  • if this is not a graphical problem please give a look at DisplayMember and ValueMember properties 如果这不是图形问题,请查看DisplayMemberValueMember属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM