Link to home
Start Free TrialLog in
Avatar of rowternet
rowternet

asked on

Selected value of a listbox

Hi,

(Winform Application, VS2008, VB.NET)

I have 2 listboxes on my form.(lstb_Drives and lstb_SelDrives)
lstb_Drives is populated from the database. It has the valuemember(Drive_ID) and displaymember(Drive Name).(Populated using a dataset)
If i select a value in the lstb_Drives  list box i am moving it to the lstb_SelDrives list box.
This part is going good.
I need to access the value member of each item in the lstb_SelDrives listbox. How can i get the value members?

Thanks
Private Sub SelectOneValue()
        Dim ht As New Hashtable
        If (lstb_Drives.SelectedIndex <> -1) Then
            MessageBox.Show(lstb_Drives.SelectedValue.ToString)
            ht.Add(lstb_Drives.SelectedValue, lstb_Drives.Text) 'lstb_Drives.SelectedValue has Drive_ID , lstb_Drives.Text has Drive Name'
            lstb_SelDrives.Items.Add(ht.Item(lstb_Drives.SelectedValue))
        End If
End Sub

'Method to retrieve the listbox values(Drive ID Values have to be retrieved from this)
Private Sub selDrivecount()
        Dim myorder As String = " and (S.Drive_id = '"
	        For selRtCount = 0 To lstb_SelDrives.Items.Count - 1
            If selRtCount = 0 Then
                lstb_SelDrives.SetSelected(selRtCount, True)
                'myorder = myorder & lstb_SelDrives.SelectedValue.ToString & "'" '''' Tried both ways but did not work
                myorder = myorder & lstb_SelDrives.SelectedItem.ToString & "'"   ' I Can retrieve the display member using this.
            Else
                lstb_SelDrives.SetSelected(selRtCount, True)
                'myorder = myorder & " or S.Drive_id='" & lstb_SelDrives.SelectedItem.ToString & "'"
                myorder = myorder & " or S.Drive_id='" & lstb_SelDrives.SelectedValue.ToString & "'"
            End If
        Next
        myorder = myorder & ")"
End Sub

Open in new window

SOLUTION
Avatar of Zhaolai
Zhaolai
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. Glad you found your way out.
Going forward, you can award points and close the question by yourself.