VB.NET How to move Listbox item up and down In Urdu (Video 5)


Project Code

Public Class Form1

Private Sub UpPictureBox_Click(sender As System.Object, e As System.EventArgs) Handles UpPictureBox.Click

If CityListBox.SelectedIndex = -1 Then

MessageBox.Show("Select an item to move", "No item selected", MessageBoxButtons.OK, MessageBoxIcon.Error)

Else

Dim newIndex As Integer = CityListBox.SelectedIndex - 1

If newIndex < 0 Then
Return
End If

Dim selectedItem As Object = CityListBox.SelectedItem

CityListBox.Items.Remove(selectedItem)

CityListBox.Items.Insert(newIndex, selectedItem)

CityListBox.SetSelected(newIndex, True)


End If

End Sub

Private Sub DownPictureBox_Click(sender As System.Object, e As System.EventArgs) Handles DownPictureBox.Click

If CityListBox.SelectedIndex = -1 Then

MessageBox.Show("Select an item to move", "No item selected", MessageBoxButtons.OK, MessageBoxIcon.Error)

Else

Dim newIndex As Integer = CityListBox.SelectedIndex + 1

If newIndex >= CityListBox.Items.Count Then
Return
End If

Dim selectedItem As Object = CityListBox.SelectedItem

CityListBox.Items.Remove(selectedItem)

CityListBox.Items.Insert(newIndex, selectedItem)

CityListBox.SetSelected(newIndex, True)


End If
End Sub
End Class

Comments