简体   繁体   中英

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.

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...

I used a few properties and .net String.SubString method instead of the old 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 .

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 .

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
  • you can use SelectedIndexChanged instead of DropDown and DropDownClosed event
  • if this is not a graphical problem please give a look at DisplayMember and ValueMember properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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