ListBox SelectedItem & SelectedIntex problem - different result!?

  • Thread starter Thread starter Wolfgang Kaml
  • Start date Start date
W

Wolfgang Kaml

Hello everybody,

seems like I got a challenging problem with the Listbox that really gets me
confused. Please have a look at the following code and output during runtime
from the Interactive Command Window:
--------------------------------------------------
? Me.LstLeistungen.GetType.FullName
"System.Windows.Forms.ListBox"

? Me.LstLeistungen.Items.Item(0).TEXT
"StringAtPos0" {String}
String: "StringAtPos0"
? Me.LstLeistungen.Items.Item(7).TEXT
"StringAtPos7" {String}
String: "StringAtPos7"


? Me.LstLeistungen.SelectedIndex
7
? Me.LstLeistungen.Items.Item(Me.LstLeistungen.SelectedIndex).TEXT 'just
looking at this statement....
"StringAtPos7" {String}
String: "StringAtPos7"
? Me.LstLeistungen.SelectedItem.TEXT
'and looking at this statement at the same time, the output does not make
sense. HELP!!!
"StringAtPos0" {String}
String: "StringAtPos0"
--------------------------------------------------
As you can see, SelectedIndex reports that item at index 7 is currently
selected, which is the true output looking at the GUI. The item at index 7
shows as selected in the listbox. However, for some reason, if I try to
retrieve that object with using the SelectedItem function, I get the object
at index position 0.

Does that make any sense whatsoever? Any idea, as of why that could
happen? - No overloading occured, I did not derive the Listbox, nothing.

Thank you for any help on that!
Wolfgang
 
Not sure what the problem is there, but you could try
ListBox.Items(SelectedIndex) to get the item instead of using SelectedItems.
 
he did that and it worksbut the selected item gives me correct results 2, it gives the object in
this case a string.

strange
 
One more thing I figured: The fact that I use drag & drop functionality on
that Listbox has an impact on the SelectedItem vs. SelectedIndex
functionality. I am not sure, why. As soon as I turn Drag & Drop for that
Listbox off, SelectedIndex and SelectedItem return the "same" results, as
expected. SelectedItem gives me the object at the index position that
SelectedIndex points to.

However, as soon as I activate Drag & Drop, SelectedItem always retrieves
the object at the first position, not at the position that SelectedIndex
points to.
???
 
Hi Wolfgang,

This is a known issue and I have reported it. I can not guarantee if the
issue will be fixed.

It is caused by that the SelectedItem gets updated on the first MouseDown
event in the ListBox. After that, it only gets updated by the MouseUp
event. DragDrop eats the MouseUp event. This means that the SelectedItem
never gets updated after the first mouse click.

Thank you for you report.
Now you can try Robin's suggestion to see if it works for you.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Wolfgang,

Did the problem resolved?
If you have any concern on this problem, please post here.


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Peter,

I will try using the workaround over the weekend and keep you posted. For
now, I turned off Drag & Drop. Meanwhile, I ran into a couple more problems
with the ComboBox control when DropDownStyle = DropDown. Reading the
SelectedItem shows Nothing once the Text is being changed by the user. Also,
setting the text to something else then in the ObjectContainer will not
work. Strange, MyComboBox.Text will read "MyListItem AndModifiedText", but
the list will display "MyListItem". I will post them on separate threads to
be able to track them later if needed.

Thank you,
Wolfgang
 
Hi Wolfgang,
I will try using the workaround over the weekend and keep you posted. For
I look forward to your test result.
now, I turned off Drag & Drop. Meanwhile, I ran into a couple more problems
with the ComboBox control when DropDownStyle = DropDown. Reading the
SelectedItem shows Nothing once the Text is being changed by the user. Also,
setting the text to something else then in the ObjectContainer will not

Since the user changed the value to an value that is not in the databinding
collection, e.g. a dataset.
In this case the selectedindex will be -1, that is to say the text user
inputed did not match any one in the columns that bind to the combobox with
the Dataset.
work. Strange, MyComboBox.Text will read "MyListItem AndModifiedText", but
the list will display "MyListItem". I will post them on separate threads to
be able to track them later if needed.
I can not reproduce the problem, can you post some code for me to reproduce
the problem.
I add a combobox onto the form and bind the combobox on an dataset, bind
the displaymember to one field of the dataset.


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
Reply-To: "Wolfgang Kaml" <[email protected]>
From: "Wolfgang Kaml" <[email protected]>
References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
 
Using
objMyObject = Me.MyList.Items.Item(Me.MyList.SelectedIndex)
instead of
objMyObject = Me.MyList.SelectedItem
worked as expected when using Drag & Drop features on the list.


However, I ran into another problem, that is
Private Sub MyList_DoubleClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyList.DoubleClick
will not pick up and DoubleClick event anymore.

Any idea of how to fix that?

Thank you!
Wolfgang
 
Hi Wolfgang,
However, I ran into another problem, that is
Private Sub MyList_DoubleClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyList.DoubleClick
will not pick up and DoubleClick event anymore.
It is strange that I can not reproduce the problem.

Here is my test code.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ListBox1.AllowDrop = True
Dim i As Integer
For i = 1 To 10
ListBox1.Items.Add("Item" + i.ToString())
Next
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ListBox1.DoubleClick
Debug.WriteLine("double clicked")
End Sub
End Class

You may have a try.
Can you post your code for me to reproduce the problem?
You may modify your code as long as it can reproduce the problem.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Wolfgang,

I have done some research and find that you can workaround the problem the
Clicks properties.
You may try to make change your code as follows.
Private Sub LstEinnahmen_MouseDown(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.MouseEventArgs) Handles LstEinnahmen.MouseDown
If e.Clicks = 1 Then
Me.LstEinnahmen.DoDragDrop("FromListBoxLStEinnahmen",
DragDropEffects.Move)
End If
End Sub

So that you can fire the DoubleClick event.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
Reply-To: "Wolfgang Kaml" <[email protected]>
From: "Wolfgang Kaml" <[email protected]>
References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#$YebF#[email protected]>
<[email protected]>
 
Peter,

You are ingenious! Now I am able to use the Drag & Drop on the Listbox
without any limitations! Great! - Thanks!

I Hope, MS fixes those bugs anyways!

Wolfgang
 
Back
Top