Hi,
I have couple of listboxes and a couple of combo box on a form. The
listboxes are databound. I am trying to clear the selection of the listboxes
if they are not selected by using ListBox.ClearSelected() but in vain.

The following lines are in the constructor of the form.

Listbox.DataSource = myDataSet;
Listbox.ClearSelected().

Can anyone please suggest as to how to clear the selection of the listbox if
not selected.

Thanks in advance.

RE: Clear Selection of a listbox by PrasadMCAD

PrasadMCAD
Mon Sep 19 04:36:04 CDT 2005

What you mean by clear the selection of the listbox if not selected. ?
DO you want to clear (not remove) the selected item from the listbox?


"pramod" wrote:

> Hi,
> I have couple of listboxes and a couple of combo box on a form. The
> listboxes are databound. I am trying to clear the selection of the listboxes
> if they are not selected by using ListBox.ClearSelected() but in vain.
>
> The following lines are in the constructor of the form.
>
> Listbox.DataSource = myDataSet;
> Listbox.ClearSelected().
>
> Can anyone please suggest as to how to clear the selection of the listbox if
> not selected.
>
> Thanks in advance.

RE: Clear Selection of a listbox by TaylorMichaelL

TaylorMichaelL
Mon Sep 19 05:55:04 CDT 2005

It could be caused by the fact that you're binding the data to the control in
the constructor. If you did this through the designer then it is also
probably doing the binding in InitializeComponent as well which might occur
after your constructor initialization (since you didn't post the entire
constructor it is hard to say). You should postpone your databinding to the
OnLoad event. You can also write a simple handler for the ListBox'es
OnSelectionChanged event and see when it is being called.

Michael Taylor - 9/19/05

"pramod" wrote:

> Hi,
> I have couple of listboxes and a couple of combo box on a form. The
> listboxes are databound. I am trying to clear the selection of the listboxes
> if they are not selected by using ListBox.ClearSelected() but in vain.
>
> The following lines are in the constructor of the form.
>
> Listbox.DataSource = myDataSet;
> Listbox.ClearSelected().
>
> Can anyone please suggest as to how to clear the selection of the listbox if
> not selected.
>
> Thanks in advance.

RE: Clear Selection of a listbox by pramod

pramod
Mon Sep 19 18:24:05 CDT 2005

Hi,
Sorry if I was not clear enough.

Clear selection mean when the listbox does not have focus, then one of the
items in the list is highlighted. This is misleading to the end user. As I
mentioned there are couple of combo boxes and listboxes, eve though one of
the combo boxes is selected (focused), one of the items in the list box is
still highlighted. The desire result should be that none of the list box
items should be highlighted if it does not have focus.

I tried one of the solutions where in setting the Listbox.SelectedItem =
null on the OnPaint method of the form. This Clears the Selection but the
problem is that if you want to select an item in the listbox then 2 clicks
are required instead of one.

I tried postponing the databinding to the OnLoad event, but the same issue
exits.

Hope this is clear enough.

Thanks.

"TaylorMichaelL" wrote:

> It could be caused by the fact that you're binding the data to the control in
> the constructor. If you did this through the designer then it is also
> probably doing the binding in InitializeComponent as well which might occur
> after your constructor initialization (since you didn't post the entire
> constructor it is hard to say). You should postpone your databinding to the
> OnLoad event. You can also write a simple handler for the ListBox'es
> OnSelectionChanged event and see when it is being called.
>
> Michael Taylor - 9/19/05
>
> "pramod" wrote:
>
> > Hi,
> > I have couple of listboxes and a couple of combo box on a form. The
> > listboxes are databound. I am trying to clear the selection of the listboxes
> > if they are not selected by using ListBox.ClearSelected() but in vain.
> >
> > The following lines are in the constructor of the form.
> >
> > Listbox.DataSource = myDataSet;
> > Listbox.ClearSelected().
> >
> > Can anyone please suggest as to how to clear the selection of the listbox if
> > not selected.
> >
> > Thanks in advance.