selection Problem in listbox when contains ,More than one same data [modified]
-
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded lst_sample.Items.Add("Anand") lst_sample.Items.Add("Anbu") lst_sample.Items.Add("Ashok") lst_sample.Items.Add("Anand") End Sub Private Sub lst_sample_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles lst_sample.SelectionChanged Dim index As Integer = 0 index = lst_sample.SelectedIndex MsgBox(index) But when I have several value that hold the same value (for example "Anand" twotimes), the selection in the ListBox has a weird behaviour : it select 2 elements instead of the only the one on which I clicked. Moreover, when I click to another listBoxItem, it doesn't unfocus the previous selected one. Then it is impossible to see which item is actually selected, and impossible to get the SelectedIndex value. Has someone an idea?
modified on Monday, July 19, 2010 9:01 AM
-
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded lst_sample.Items.Add("Anand") lst_sample.Items.Add("Anbu") lst_sample.Items.Add("Ashok") lst_sample.Items.Add("Anand") End Sub Private Sub lst_sample_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles lst_sample.SelectionChanged Dim index As Integer = 0 index = lst_sample.SelectedIndex MsgBox(index) But when I have several value that hold the same value (for example "Anand" twotimes), the selection in the ListBox has a weird behaviour : it select 2 elements instead of the only the one on which I clicked. Moreover, when I click to another listBoxItem, it doesn't unfocus the previous selected one. Then it is impossible to see which item is actually selected, and impossible to get the SelectedIndex value. Has someone an idea?
modified on Monday, July 19, 2010 9:01 AM
I think the problem is that your ListBox has no way of differentiating between the two "Anand" records. It sees them as the same thing. Try creating and populating a class that inherits from ObservableCollection. Then bind the ListBox to the collection. This way, each ListItem will be unique.
-
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded lst_sample.Items.Add("Anand") lst_sample.Items.Add("Anbu") lst_sample.Items.Add("Ashok") lst_sample.Items.Add("Anand") End Sub Private Sub lst_sample_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles lst_sample.SelectionChanged Dim index As Integer = 0 index = lst_sample.SelectedIndex MsgBox(index) But when I have several value that hold the same value (for example "Anand" twotimes), the selection in the ListBox has a weird behaviour : it select 2 elements instead of the only the one on which I clicked. Moreover, when I click to another listBoxItem, it doesn't unfocus the previous selected one. Then it is impossible to see which item is actually selected, and impossible to get the SelectedIndex value. Has someone an idea?
modified on Monday, July 19, 2010 9:01 AM
The problem is that the ListBox cannot differentiate between the two "Anand" values, so when you click on one it shows all "Anand"'s being selected. You'd get this same effect if you create an object and added it to the list more than once like so. I'll guess that these are names. So, my solutions would be to create a Person object like this:
class Person
{
public int ID { get; set; }
public string Name { get; set; }public Person(int id, string name) { this.ID = id; this.Name = name; } }
Then when adding the items to the list it would look like this:
lst_sample.Items.Add(new Person(1, "Anand"))
lst_sample.Items.Add(new Person(2, "Anand"))Then set the Listboxes DisplayMemberPath = "Name".