deselect an item when a particular item is selected in listbox RRS feed

  • Question

  • I have listbox on a windows form in VB.NET, the list box contains list of jobs, which are populated from a database.
    The values selected on the listbox are then used to built up a SQL statement which is then sent to create a report.
    My problem is, I want the list box to deselect all other items when one particular item is selected. Basically my problem is, when I selected item 0 (which is list of all jobs)I want all other items to be unselected and if any other items are selected, Item 0 must be unselected. 
    Item 0 = is basically and group of jobs.
    remaining Items = individual jobs.
    Below is the code which i'm using to load listbox

        Public Sub CommandCollection()
                Dim connetionString As String = Nothing
                Dim connection As SqlConnection
                Dim command As SqlCommand
                Dim adapter As New SqlDataAdapter()
                Dim ds As New DataSet()
                Dim i As Integer = 0
                ListBox1.Enabled = False
                BtnClosedJobActualQuoteViewer_Click.Enabled = True
                Dim sql As String = Nothing
                connetionString = "Data Source=DEV-TST235\SQLEXPRESS2005;Initial Catalog=TSTracker;Integrated Security=True"
                sql = "SELECT -1 JobId, '<All>' JobNumber" & _
                        " UNION " & _
                        " SELECT * from (SELECT DISTINCT Jobs.JobId,RTRIM(Jobs.JobNumber) JobNumber FROM Jobs INNER JOIN" & _
                        " Companies (NOLOCK) ON Jobs.CustomerID = Companies.CompanyID INNER JOIN" & _
                        " JobHistory (NOLOCK) ON Jobs.JobID = JobHistory.JobID" & _
                        " WHERE  (JobHistory.modifiedDate > '" & StartDate.Value.ToShortDateString & "') AND (JobHistory.modifiedDate < '" & EndDate.Value.ToShortDateString & "') AND (JobHistory.Description = 'Job Closed')" & _
                        ")a ORDER BY JobNumber"
                connection = New SqlConnection(connetionString)
                    command = New SqlCommand(sql, connection)
                    adapter.SelectCommand = command
                    ListBox1.DataSource = ds.Tables(0)
                    ListBox1.ValueMember = "JobId"
                    ListBox1.DisplayMember = "JobNumber"
                    If ListBox1.Items.Count > 1 Then
                        ListBox1.Enabled = True
                        ListBox1.Enabled = False
                        ListBox1.DataSource = Nothing
                        BtnClosedJobActualQuoteViewer_Click.Enabled = False
                    End If
                Catch ex As Exception
                    MessageBox.Show("Can not open connection ! ")
                End Try
            End Sub

    and below is the code where i can manage to unselect the selected items when an item at index=0 is selected which is half of the answer but i also want it to happen in the other way, i mean if other items as selected other then the item at index = 0 then the item at index= 0 must be unselected.

         Dim index As Integer
                    index = ListBox1.SelectedIndex
                    If index = 0 Then
                        ListBox1.SetSelected(0, True)
                    ElseIf index <> 0 Then
                        ListBox1.SetSelected(0, False)
                    End If
                End If
    Thursday, March 10, 2016 7:17 PM


  • Hi harsha kondeti,

    According to your requirement, you could try to use the checkedlistbox.


        Private Sub ChangeListBoxCheck(listbox As CheckedListBox, check As Boolean)
            For x = 0 To CheckedListBox1.Items.Count - 1
                CheckedListBox1.SetItemChecked(x, check)
            Next x
        End Sub
        Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
            If CheckedListBox1.Items.Count > 0 Then
                If CheckedListBox1.GetSelected(0) Then
                    'check all item
                    ChangeListBoxCheck(CheckedListBox1, True)
                    Dim index As Int32 = CheckedListBox1.SelectedIndex
                    'uncheck all item
                    ChangeListBoxCheck(CheckedListBox1, False)
                    'check the index
                    CheckedListBox1.SetItemChecked(index, True)
                End If
            End If
        End Sub


    Click ‘All’

    Click ‘other’



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 11, 2016 9:36 AM