Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. selection Problem in listbox when contains ,More than one same data [modified]

selection Problem in listbox when contains ,More than one same data [modified]

Scheduled Pinned Locked Moved WCF and WF
databasehelptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rajasekarmani
    wrote on last edited by
    #1

    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

    D J 2 Replies Last reply
    0
    • R rajasekarmani

      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

      D Offline
      D Offline
      D Dubya
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R rajasekarmani

        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

        J Offline
        J Offline
        Jeremy Hutchinson
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups