Un-Highlight First Item in ListBox?

  • Thread starter Vagabond Software
  • Start date
V

Vagabond Software

I'm using VS2003 on .NET 1.1

When I assign a DataSource to my ListBox, the first Item in the Items
collected is automatically selected. I want to avoid this behavior.

I'm halfway to a solution by intercepting the DataSourceChanged event and
de-selecting the Item at the SelectedIndex. This works as desired. There
are no items selected after the DataSource property is changed.

However, the ListBox still shows the first Item as being Highlighted. That
is now misleading because the first Item is, in fact, NOT selected.

Everything operates as desired. The end-user can click on the first Item
and that will select it, but it is misleading that the first Item already
"appears" to be selected because of the Highlight.

Any clues as to how to un-highlight the first Item in a ListBox? Here is
the code used to de-select the first item:

Private Sub lstboxColumns_DataSourceChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles lstboxColumns.DataSourceChanged

If lstboxColumns.SelectedItems.Count > 0 Then
lstboxColumns.SetSelected(lstboxColumns.SelectedIndex, False)
End If
End Sub

Carl
 
T

Truong Hong Thi

If the item is not selected, it is not hi-light. I believe that what is
said "hightlight" is the focus clues. It is different from highlighting
where often the background of the item changes.
To hide the focus rectangle, just set focus to other control. For
example, if you have another text box control, call that text
box.Select() method.

Thi
 
V

Vagabond Software

Truong Hong Thi said:
If the item is not selected, it is not hi-light. I believe that what is
said "hightlight" is the focus clues. It is different from highlighting
where often the background of the item changes.
To hide the focus rectangle, just set focus to other control. For
example, if you have another text box control, call that text
box.Select() method.

Thi

Here is an image of a "highlighted" item in a listbox that is NOT selected:

http://home.san.rr.com/vagabondia/images/tmp/highlighted_listbox.gif

Looking at the image, you can see that a Caption from the "FilterBy" ListBox
is selected because the corresponding ColumnName is displayed in the
expression TextBox just above the ListBoxes.

However, you can also see that no items are selected in the Values ListBox
because it has not been appended to the current expression string in the
TextBox. We can perform any evaluations and it will be confirmed that no
items are selected in the Values ListBox. Yet, the "highlight" remains.

Carl
 
T

Truong Hong Thi

It is the problem of event firing order. After you deslect the item in
lstboxColumns_DataSourceChanged, no item is selected. However, the
first item get selected *after* that. As the result, DataSourceChanged
is not the place for you to do the deselecting.

One solution of this is: deselect right after you set the data source.
For example:
lstboxColumns.DataSource = somethingNew;
// Deselect here, not in DataSourceChanged event
lstboxColumns.SelectedIndex = -1; // or lstboxColumns.ClearSelected();

lstboxColumns.SetSelected is also OK, but it requires a check first,
and is often used for multi-selection listbox.

Hope it helps,
Thi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top