Is there a way to set the item in a combobox with text only? I have a combobox and a listview I have names in both I want the combobox to change to the same name as that in the listview when the listview item is selected. The listview does not have the name index. Possible?
It kind of depends on how you have populated your list controls. If you have added Strings to the Items collections then you can use the SelectedItem. If you have set the datasource to a list or a datatable or some collection of objects then it may get more complicated. Here is an example of using strings to accomplish this task. Note that I am handling the SelctedIndexChanged event on the listbox...
The listview is populated from a sqlDataReader. I'm using this
cmbCostCentre.SelectedItem = lstCapture.SelectedItems(0).ToString
Nothings changing in the combobox I suspect theres no index in the listview to help it. Isn't there something that could match the text if true then set the combobox?
i think the listbox has some qwerks when selecting items. i don't use it. i would rather format a datagridview to look like a listview intead. but you might get some use out of this or may give you some ideas to get what you want
Me.ListView1.FullRowSelect = True
Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
Dim hti As ListViewHitTestInfo = ListView1.HitTest(e.Location)
Dim subitemindex As Integer = hti.Item.SubItems.IndexOf(hti.SubItem)
Dim val As String = hti.Item.SubItems(subitemindex).Text
If subitemindex = 0 Then
MsgBox("item index = " & subitemindex.ToString & " item text = " & val)
MsgBox("subitem index = " & subitemindex.ToString & " subitem text = " & val)
Jeff - www.srsoft.us
FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us
Hi Ivan Jericevich,
You can try these to choose the comboboxitem.
In that way, you should keep combobox's index the same as the listview.
Or use the text to select :
In addtion, use ListView1_SelectedIndexChanged event.
Does this works for you? If you have any further questions or concerns, please update the thread and we will have a future discussion.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
combobox1.Text = (lstview1.SelectedItems(0).SubItems(6).Text)