The List Manager Project: Inserting

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 DeleteItem() Handles DeleteToolStripMenuItem.Click
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 'DeleteItem
See the complete code at this point.
INDEX, Introduction, Build the Menu, Add items to a listbox, Delete items from the listbox, Save: Write the contents of the list box to a file, Open File: Read file and add to list box, Ask the user if he wants to save, When to ask the user if he wants to save, The User closes the program
Next lesson: List Manager application Finishing Touches

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Delete items from the listbox