ListBox.ClearSelected Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Desseleciona todos os itens no ListBoxarquivo .
public:
void ClearSelected();
public void ClearSelected();
member this.ClearSelected : unit -> unit
Public Sub ClearSelected ()
Exemplos
O exemplo de código seguinte demonstra como usar a SelectedIndex propriedade com a TopIndex propriedade para mover o item atualmente selecionado para o topo da lista de itens na área de exibição do ListBox. O exemplo demonstra ainda como remover itens usando o método RemoveAt da classe System.Windows.Forms.ListBox.ObjectCollection, e como limpar toda a seleção de itens usando o método ClearSelected. O código primeiro move o item ListBox atualmente selecionado para o topo da lista. O código remove então todos os itens antes do item atualmente selecionado e limpa todas as seleções no ListBoxarquivo . Este exemplo exige que um ListBox item contendo seja adicionado a um formulário e que um item esteja atualmente selecionado no ListBoxformulário .
private:
void RemoveTopItems()
{
// Determine if the currently selected item in the ListBox
// is the item displayed at the top in the ListBox.
if ( listBox1->TopIndex != listBox1->SelectedIndex )
// Make the currently selected item the top item in the ListBox.
listBox1->TopIndex = listBox1->SelectedIndex;
// Remove all items before the top item in the ListBox.
for ( int x = (listBox1->SelectedIndex - 1); x >= 0; x-- )
{
listBox1->Items->RemoveAt( x );
}
// Clear all selections in the ListBox.
listBox1->ClearSelected();
}
private void RemoveTopItems()
{
// Determine if the currently selected item in the ListBox
// is the item displayed at the top in the ListBox.
if (listBox1.TopIndex != listBox1.SelectedIndex)
// Make the currently selected item the top item in the ListBox.
listBox1.TopIndex = listBox1.SelectedIndex;
// Remove all items before the top item in the ListBox.
for (int x = (listBox1.SelectedIndex -1); x >= 0; x--)
{
listBox1.Items.RemoveAt(x);
}
// Clear all selections in the ListBox.
listBox1.ClearSelected();
}
Private Sub RemoveTopItems()
' Determine if the currently selected item in the ListBox
' is the item displayed at the top in the ListBox.
If listBox1.TopIndex <> listBox1.SelectedIndex Then
' Make the currently selected item the top item in the ListBox.
listBox1.TopIndex = listBox1.SelectedIndex
End If
' Remove all items before the top item in the ListBox.
Dim x As Integer
For x = listBox1.SelectedIndex - 1 To 0 Step -1
listBox1.Items.RemoveAt(x)
Next x
' Clear all selections in the ListBox.
listBox1.ClearSelected()
End Sub
Observações
Chamar este método é equivalente a definir a SelectedIndex propriedade para menos um (-1). Pode usar este método para desmarcar rapidamente todos os itens da lista.