• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

Clearing a ComboBox and ListBox

How can you clear a listbox of all its ticks?

How can you reset the ComboBox to its original value. In my case "<<SELECT VALUE>>"?
0
PeterErhard
Asked:
PeterErhard
1 Solution
 
123654789987Commented:
U can use Listbox.Items.remove to remove the items. U can use it in a loop like

for i = 0 to listbox.Items.Count -1
listBox.Items.Remove(i)
Next i

To reset a combobox use can have a empty value added in the combobox while creating it

combobox.items.Add(" ");
Then when u have to reset a value u can select that index.

combobox.SelectedIndex = 1
0
 
armoghanCommented:
instead of loop use
listView1.Items.Clear () to reset any collection



0
 
Fahad MukhtarDistinguished EngineerCommented:
'How can you reset the ComboBox to its original value. In my case "<<SELECT VALUE>>"?
ComboBox1.SelectedIndex = 0 'usually the orignal value is the first item in the combobox items collection not the second one

'How can you clear a listbox of all its ticks?
ListBox.Items.Clear()
ComboBox.Items.Clear()

are you trying to clear the items and then reset the ComboBox to its original value??? then use this
ComboBox.Items.Clear()
ComboBox1.Items.Add("<<SELECT VALUE>>")
ComboBox1.SelectedIndex = 0
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PeterErhardAuthor Commented:
Neither work as I expected:

listView1.Items.Clear removes all the items within the listbox. I want to retain all the items but just remove the selected tickes.

ComboBox1.SelectedIndex = 0 gives me the following error - "Specified argument was out of the range of valid values. Paramater name: '0' is not a valid value for index!"
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
To clear the selected items in a ListBox use:

    ListBox1.SelectedIndex = -1

~IM
0
 
PeterErhardAuthor Commented:
I just tried this and it didn't work. The ticks still remain.

I've increased the points to 500!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Worked fine for me.  Are you doing anything special with the ListBox?

~IM
0
 
PeterErhardAuthor Commented:
Nope - the only thing I'm doing is populating it from the database. When I use your code nothing happens.

    Private Function LoadEthnicGroups()

        Dim Index As Byte
        Dim cmd As SqlCommand = cnn.CreateCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "procLoadEthnicGroupOptions"

        Dim reader As SqlDataReader = cmd.ExecuteReader

        With reader
            While reader.Read
                ListBox_EthnicGroup.Items.Add(.GetValue(.GetOrdinal("LookUp_EthnicGroupIDAuto")))
                ListBox_EthnicGroup.Items.Item(Index) = .GetValue(.GetOrdinal("EthnicGroup"))
                Index = Index + 1
            End While
        End With

        reader.Close()

    End Function
0
 
LandyJCommented:
Try:

dim i as integer
For i = 0 To CheckedListBox.Items.Count - 1
    CheckedListBox.SetItemChecked(i, False)
Next i

For the ComboBox, if you only want to set the selection, and not clear the box, use Desp's answer
ComboBox.SelectedIndex = 0

To clear and reset, you part 2 of Desp's answer:
ComboBox.Items.Clear()
ComboBox1.Items.Add("<<SELECT VALUE>>")
ComboBox1.SelectedIndex = 0


Hope this helps,
Landy
0
 
PeterErhardAuthor Commented:
Thanks for that - all works :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now