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. Visual Basic
  4. Strange behavior in listbox control - VB.NET 2.0

Strange behavior in listbox control - VB.NET 2.0

Scheduled Pinned Locked Moved Visual Basic
helpcsharpdatabaseregex
7 Posts 2 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.
  • M Offline
    M Offline
    Marcus J Smith
    wrote on last edited by
    #1

    I have several list box controls in an application. Some I just add the items through a loop and the others I actually bind a datasource to. I am experiencing some strange behavior from the databound controls. If I click or scroll through the list the selected item will jump around so for example...

    The items are...
    First Item
    Second Item
    Third Item

    I select Second Item no problem. I select the Third Item and the selected item will switch to the First Item. I try to select the Second Item again and it bounces back to the First Item then I select the Third Item no problem. That can happen in any order so pay no attention to First, Second, Third, First or any pattern like that. Also this will occur with a click or a key press. I break pointed at the selected index changed event and if I were to select the Third item and the First Item catches the selection then that is the value that comes across at the selected index changed event. Is this an issue with the databound control or is there some concept I am missing here?


    CleaKO

    "Now, a man would have opened both gates, driven through and not bothered to close either gate." - Marc Clifton (The Lounge)

    C 1 Reply Last reply
    0
    • M Marcus J Smith

      I have several list box controls in an application. Some I just add the items through a loop and the others I actually bind a datasource to. I am experiencing some strange behavior from the databound controls. If I click or scroll through the list the selected item will jump around so for example...

      The items are...
      First Item
      Second Item
      Third Item

      I select Second Item no problem. I select the Third Item and the selected item will switch to the First Item. I try to select the Second Item again and it bounces back to the First Item then I select the Third Item no problem. That can happen in any order so pay no attention to First, Second, Third, First or any pattern like that. Also this will occur with a click or a key press. I break pointed at the selected index changed event and if I were to select the Third item and the First Item catches the selection then that is the value that comes across at the selected index changed event. Is this an issue with the databound control or is there some concept I am missing here?


      CleaKO

      "Now, a man would have opened both gates, driven through and not bothered to close either gate." - Marc Clifton (The Lounge)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Yes, if you databind to your control, the selection is lost. I suspect you are databinding again when the selectio nchanges.

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      M 1 Reply Last reply
      0
      • C Christian Graus

        Yes, if you databind to your control, the selection is lost. I suspect you are databinding again when the selectio nchanges.

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        M Offline
        M Offline
        Marcus J Smith
        wrote on last edited by
        #3

        Thanks for the suggestion, I just looked into that and it is not hitting the databind code again after the initial databind until the correct place. One thing I just noticed as a pattern. If I have 15 items in a listbox and I try to scroll through them it goes in this type of order.

        1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, etc...

        Does that ring any bells?


        CleaKO

        "Now, a man would have opened both gates, driven through and not bothered to close either gate." - Marc Clifton (The Lounge)

        C 1 Reply Last reply
        0
        • M Marcus J Smith

          Thanks for the suggestion, I just looked into that and it is not hitting the databind code again after the initial databind until the correct place. One thing I just noticed as a pattern. If I have 15 items in a listbox and I try to scroll through them it goes in this type of order.

          1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, etc...

          Does that ring any bells?


          CleaKO

          "Now, a man would have opened both gates, driven through and not bothered to close either gate." - Marc Clifton (The Lounge)

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          No, that does sound strange. It sounds like your data source is messed up ?

          Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          M 1 Reply Last reply
          0
          • C Christian Graus

            No, that does sound strange. It sounds like your data source is messed up ?

            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            M Offline
            M Offline
            Marcus J Smith
            wrote on last edited by
            #5

            I think I explained that incorrectly, when I said that it goes in the order of 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, etc... I mean that the selected index will work that way, the highlighted item. So there will be a total of say 15 items in the box but when I scroll through the highlighted item will be the first item, second item, third item, fourth item, fifth item, back to the first item then through the sixth and back to the first again. Here is the code I use to load the listboxes. uxDepartments.DataSource = Nothing uxDepartments.Items.Clear() uxDepartments.SelectedIndex = -1 _DepartmentTable = New DataTable _DepartmentTable = GetDepartmentNames uxDepartments.DataSource = _DepartmentTable uxDepartments.ValueMember = "DepartmentNameID" uxDepartments.DisplayMember = "DepartmentName" 'Need to clear the listboxes with multi selection enabled uxDepartmentsForList.SelectedItems.Clear() If _DepartmentTable.Rows.Count = 2 Then uxDepartmentsForList.SelectedIndex = 1 Else uxDepartmentsForList.SelectedIndex = -1 End If


            CleaKO

            "Now, a man would have opened both gates, driven through and not bothered to close either gate." - Marc Clifton (The Lounge)

            C 1 Reply Last reply
            0
            • M Marcus J Smith

              I think I explained that incorrectly, when I said that it goes in the order of 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, etc... I mean that the selected index will work that way, the highlighted item. So there will be a total of say 15 items in the box but when I scroll through the highlighted item will be the first item, second item, third item, fourth item, fifth item, back to the first item then through the sixth and back to the first again. Here is the code I use to load the listboxes. uxDepartments.DataSource = Nothing uxDepartments.Items.Clear() uxDepartments.SelectedIndex = -1 _DepartmentTable = New DataTable _DepartmentTable = GetDepartmentNames uxDepartments.DataSource = _DepartmentTable uxDepartments.ValueMember = "DepartmentNameID" uxDepartments.DisplayMember = "DepartmentName" 'Need to clear the listboxes with multi selection enabled uxDepartmentsForList.SelectedItems.Clear() If _DepartmentTable.Rows.Count = 2 Then uxDepartmentsForList.SelectedIndex = 1 Else uxDepartmentsForList.SelectedIndex = -1 End If


              CleaKO

              "Now, a man would have opened both gates, driven through and not bothered to close either gate." - Marc Clifton (The Lounge)

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              CleaKO wrote:

              _DepartmentTable = New DataTable

              This is obviously a waste of time, but it won't cause your issue. And this is a winforms app, and this code does not run again ? What's your selected index changed look like ?

              Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

              M 1 Reply Last reply
              0
              • C Christian Graus

                CleaKO wrote:

                _DepartmentTable = New DataTable

                This is obviously a waste of time, but it won't cause your issue. And this is a winforms app, and this code does not run again ? What's your selected index changed look like ?

                Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                M Offline
                M Offline
                Marcus J Smith
                wrote on last edited by
                #7

                Christian Graus wrote:

                This is obviously a waste of time, but it won't cause your issue.

                Yes I am a little off and on with the new object setting depending on how I set the object later. I believe this was written before I quit with the extra step. :) If uxDepartments.SelectedIndex > 0 Then uxDepartmentEdit.Text = CType(uxDepartments.SelectedItem, DataRowView)("DepartmentName").ToString.Trim uxAddDepartmentName.Enabled = False uxUpdateDepartmentName.Enabled = True uxDeleteDepartmentName.Enabled = True Else uxDepartmentEdit.Text = "" uxAddDepartmentName.Enabled = True uxUpdateDepartmentName.Enabled = False uxDeleteDepartmentName.Enabled = False End If


                CleaKO

                "Now, a man would have opened both gates, driven through and not bothered to close either gate." - Marc Clifton (The Lounge)

                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