Zebra0.com

visual-basic listmanager

After creating the menu, we added the listbox and wrote the code to insert items in the listbox.

After we have a menu, and know that inserting into the list box works, we are ready to write the code for delete in the same way.

There are two situations that could cause an error: there is nothing in the list box to delete; or there is nothing selected.

The code looks for both before trying to delete.

After deleting, the first item in the list box is selected. (If there is anything in the list box.)

Double click on the Delete menu item and add the code below:

Code for Delete Menu Item

Private Sub mnuDelete_Click(sender As Object, e As EventArgs) Handles mnuDelete.Click
'Delete if there are items and one is selected
  If ListBox1.Items.Count > 0 And ListBox1.SelectedIndex >= 0 Then
    ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
  Else
    ListBox1.Text = ""
  End If
  If ListBox1.Items.Count > 0 Then
    ListBox1.SelectedIndex = 0
  End If
End Sub
See the complete code at this point.

NEXT: Build the Menu