none
Set item in combobox with text from a selected listview item?

    Question

  •  
    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?
    Monday, January 19, 2009 11:53 PM

Answers

  • Found it! :P

    combobox1.Text = (lstview1.SelectedItems(0).SubItems(6).Text)

    • Marked as answer by Ivan J Wednesday, January 21, 2009 10:14 AM
    • Unmarked as answer by Ivan J Wednesday, January 28, 2009 6:48 AM
    • Marked as answer by Ivan J Wednesday, January 28, 2009 6:49 AM
    Wednesday, January 21, 2009 10:14 AM

All replies

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

     
    Public Class Form1  
     
        'To run this code just drag a combobox and a listbox from the toolbox to the form's designer  
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
     
            Dim lst As New List(Of String)  
     
            For x As Int32 = 1 To 10  
                lst.Add("Item" & x.ToString)  
            Next 
     
            ListBox1.Items.AddRange(lst.ToArray)  
     
            lst.Sort()  
            lst.Reverse()  
     
            ComboBox1.Items.AddRange(lst.ToArray)  
     
            lst = Nothing 
     
        End Sub 
     
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As ObjectByVal e As System.EventArgs) _  
        Handles ListBox1.SelectedIndexChanged  
            'Note:  if you want to allow the combobox to be set to Nothing then remove this condition.  
            If ListBox1.SelectedIndex <> -1 Then 
                ComboBox1.SelectedItem = ListBox1.SelectedItem.ToString  
            End If 
        End Sub 
    End Class 
     
     

    -DB

    Tuesday, January 20, 2009 12:52 AM
  • 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?

    Tuesday, January 20, 2009 2:28 PM
  • 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

      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Me.ListView1.FullRowSelect = True

        End Sub

     

        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)

            Else

                MsgBox("subitem index = " & subitemindex.ToString & "  subitem text = " & val)

            End If

        End Sub


    Jeff - www.srsoft.us


    FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us
    Tuesday, January 20, 2009 3:57 PM
  • Hi Ivan Jericevich,

    You can try these to choose the comboboxitem.

            If (myListView.SelectedItems.Count = 1) Then 
                ComboBox1.SelectedIndex = myListView.SelectedIndices(0)  
            End If 

    In that way, you should keep combobox's index the same as the listview.
    Or use the text to select :

            If (ListView1.SelectedItems.Count = 1) Then 
                ComboBox1.SelectedIndex = _  
    ComboBox1.Items.IndexOf(ListView1.Items(ListView1.SelectedIndices(0)).Text)  
            End If 

    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.

     

     

    Best Regards

    Yichun Feng



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, January 21, 2009 3:08 AM
  • Found it! :P

    combobox1.Text = (lstview1.SelectedItems(0).SubItems(6).Text)

    • Marked as answer by Ivan J Wednesday, January 21, 2009 10:14 AM
    • Unmarked as answer by Ivan J Wednesday, January 28, 2009 6:48 AM
    • Marked as answer by Ivan J Wednesday, January 28, 2009 6:49 AM
    Wednesday, January 21, 2009 10:14 AM
  • My apologies Ivan.  I misread it as a Listbox you were working with.  No wonder it didn't work!
    Wednesday, January 21, 2009 5:10 PM