Listbox bug ??

  • Thread starter Thread starter Steve Cutting
  • Start date Start date
S

Steve Cutting

Hi all,

I have found that when I have a listbox bound to a datatable, the
ClearSelected method of the listbox does not work properly. It just changes
the selection to item 1 in the listbox. Calling ClearSelected again then
removes the selection.

If the listbox is unbound it works correctly. ie only one call of
ClearSelected is required.

I have tried using listbox.selectedindex = -1
This has the same effect.

Anyone else encountered this ??
Steve
 
Steve,

I am not sure however it seems one of the very well known bugs.
Did you try setting the selectedindex twice to -1 that is mostly the
solution.

I hope this helps,

Cor
 
Steve,

I am not sure however it seems one of the very well known bugs.
Did you try setting the selectedindex twice to -1 that is mostly the
solution.

Hi Cor,

Yes both options work if I call them twice. (ie either clearselected, or
..selectedindex=-1). The trouble is it only seems to work once the listbox is
visible.

I have a modal dialog form where I want the selection cleared when the form
is displayed. The above method in my form_load event works for the initial
..showdialog call of the form after it's instantiation, but not after the
form has been hidden by dialogOK/cancel and then called again with
..showdialog. On subsequent calls the first list item is always selected
(even if there was nothing selected when it was closed).

I suppose one option would be to just .dispose the form after each call and
recreate it each time I need it, seeing as it works on the first call.

thanks
Steve
 
Steve,

Probably you have your code in the load event.
Can you bring that to the New Sub after there where is written.
'Add any initialization after the InitializeComponent() call

I tried it, and than it works for me, in the load event I get the same
problem as you.

I hope this helps?

Cor
 
Steve,

Probably you have your code in the load event.
Can you bring that to the New Sub after there where is written.
'Add any initialization after the InitializeComponent() call

I tried it, and than it works for me, in the load event I get the same
problem as you.

I hope this helps?


Hi Cor,

What I have at the moment:

1. In the New sub I call my InitTable sub, which creates columns in the
datatable and then binds the listbox to it.

2. In my form_load I call a function which populates the datatable with data
from a database. Having this in the form_load means the data is refreshed
every time the form is called with .showdialog.

3. After this I have put my .selectedindex = -1 in the form_load

As I said before this works for the first call but not subsequent ones.

I just tried putting all of the above in my New sub and now it doesn't work
even for the first call. Item 1 is selected, even with 2 x .selectedindex=-1
and 2 x clearselected in the New sub after I populate the datatable.


thanks
Steve
 
* Steve Cutting said:
I have found that when I have a listbox bound to a datatable, the
ClearSelected method of the listbox does not work properly. It just changes
the selection to item 1 in the listbox. Calling ClearSelected again then
removes the selection.

If the listbox is unbound it works correctly. ie only one call of
ClearSelected is required.

I have tried using listbox.selectedindex = -1
This has the same effect.

BUG: ComboBox Does Not Clear When You Set SelectedIndex to -1
<URL:http://support.microsoft.com/?scid=kb;EN-US;327244>
 
Steve,

Do you want to select the first item or want you keep it as it was, you
confuse me, because from your previous message I understood that you did
want to keep it in the state you left it?

Cor
 
Steve,

Do you want to select the first item or want you keep it as it was, you
confuse me, because from your previous message I understood that you did
want to keep it in the state you left it?

Hi Cor,

I want nothing to be selected in the listbox whenever I call the form with
..showdialog

thanks
Steve
 
Steve,

This works without any problem for me.

Private Sub Form2_Activated(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Me.ListBox1.SelectedIndex = -1
End Sub

When it does not for you, reply than I past in the test I made.

Cor
 
Herfried,

I was assuming that as well in my first message.
However is that not that combobox textbox part?

:-)

Cor
 
Steve,

This works without any problem for me.

Private Sub Form2_Activated(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Me.ListBox1.SelectedIndex = -1
End Sub

Thanks Cor. That works :) Using clearselected works there too.

thanks again for your help
Steve
 
* "Cor Ligthert said:
I was assuming that as well in my first message.
However is that not that combobox textbox part?

Maybe it's a bug in the underlying 'ListControl' implementation that is
used by both, comboboxes and listboxes.
 
Back
Top