Strange behavior in listbox control - VB.NET 2.0
-
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 ItemI 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)
-
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 ItemI 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)
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 )
-
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 )
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)
-
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)
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 )
-
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 )
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)
-
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)
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 )
-
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 )
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)