Ciao a tutti.
Sapete come verificare se l'elemento selezionato di una listbox è vuoto?
Grazie in anticipo
Ciao a tutti.
Sapete come verificare se l'elemento selezionato di una listbox è vuoto?
Grazie in anticipo
Cosa ti restituisce quando è vuoto? Hai controllato?
No MP tecnici (non rispondo nemmeno!), usa il forum.
codice:Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged If ListBox1.SelectedItem = Nothing Then Label1.Text = "VUOTO" Else Label1.Text = "Non vuoto" End If End Sub
ci provo
dipende cosa c'è dentro il listbox. Se ci sono stringhe, tipo per esempio:
Me.ListBox1.Items.AddRange(New Object() {"", "Stefano", "Carlo", "Nicola", "Pietro", "Lucia"})
farei così:
codice:Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged Dim c As ListBox = DirectCast(sender, ListBox) Dim item As String = c.SelectedItem.ToString If String.IsNullOrWhiteSpace(item) Then Label1.Text = "VUOTO" Else Label1.Text = "Non vuoto" End If End Sub
Pietro
Ok, funziona.
Se io volessi eliminare l'item selezionato (vuoto)?
Ho provato così:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim c As ListBox = DirectCast(sender, ListBox)
Dim item As String = c.SelectedItem.ToString
If String.IsNullOrWhiteSpace(item) Then
ListBox1.Items.Remove(ListBox1.SelectedItem)
Else
End If
End Sub
Non funziona
sinceramente non so a che cosa possa servire, ma, solo per prova
codice:Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged Dim c As ListBox = DirectCast(sender, ListBox) Dim index As Integer = c.SelectedIndex If index = -1 Then Return Dim item As String = c.SelectedItem.ToString If String.IsNullOrWhiteSpace(item) Then 'Label1.Text = "VUOTO" c.Items.RemoveAt(index) Else 'Label1.Text = "Non vuoto" End If End Sub
Pietro