Removing a selected index in a list box

basicly i got everything cept for remvoing stuff from my listbox

Page 1 of 1

4 Replies - 10903 Views - Last Post: 22 July 2008 - 05:04 PM Rate Topic: -----

#1 boohickmanjc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-July 08

Removing a selected index in a list box

Post icon  Posted 21 July 2008 - 03:00 PM

This program is pretty big so Iím going to paste all the code I have. The problem I am having is trying to remove an item from the list box. so here's how it goes I add an item in the list box then I select the item and click remove and it ask me yes or no, I click yes then this comes up Argument 'key' is not a valid value. Any help would be much appreciated. Thanks

The first line of code is for my main form.
Public Class frmMain

	Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'creat the collection
		emailCollection = New Collection

	End Sub

	Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
		'display the add email form
		Dim addEmailForm As New frmAddNewName()

		'display the form
		addEmailForm.ShowDialog()

		'update the contents of the list box
		UpdateListBox()
	End Sub
	Private Sub UpdateListBox()
		'update the list box contents clear the list box
		lstNames.Items.Clear()

		'load the names in the collection into the list box
		Dim a As AddressNames
		For Each a In emailCollection
			lstNames.Items.Add(a.FullName)
		Next
		'select the first item in the list
		If lstNames.Items.Count > 0 Then
			lstNames.SelectedIndex = 0
		End If
	End Sub



	Private Sub lstNames_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstNames.SelectedIndexChanged

		'update the selected student data.
		Dim PeopleData As AddressNames
		If lstNames.SelectedIndex <> -1 Then
			'retrieve the student's data from the collection.
			Try
				PeopleData = CType(emailCollection.Item(lstNames.SelectedItem), AddressNames)
			Catch ex As Exception
				MessageBox.Show(ex.Message)
				PeopleData = Nothing
			End Try
		End If
	End Sub

	Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
		Me.Close()

	End Sub

	Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
		If lstNames.SelectedIndex = -1 Then Return
		'remove the slected person from the collection
		Dim intIndex As Integer
		If lstNames.SelectedIndex <> -1 Then
			If MessageBox.Show("Are ya sure?", "Confirm deletion", _
							   MessageBoxButtons.YesNo) = _
			Windows.Forms.DialogResult.Yes Then
				'retrieve the persons's data from the collection.
				intIndex = lstNames.SelectedIndex
				Try
					emailCollection.Remove(lstNames.SelectedIndex.ToString())
					lstNames.Items.Remove(intIndex)
					UpdateListBox()
				Catch ex As Exception
					MessageBox.Show(ex.Message)
				End Try
			End If
		End If
	End Sub
End Class



Next is the code for my add form
Public Class frmAddNewName

	Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
		Dim emailRecord As AddressNames

		'create the email address instance
		emailRecord = New AddressNames()
		'get the data from the form
		GetData(emailRecord)
		'save the record.
		AddRecord(emailRecord)
		'confirm that the record was saved
		MessageBox.Show("Record added.", "Confirmation, Good Job!!!")
		'clear the form
		ClearForm()
	End Sub
	Private Sub GetData(ByVal a As AddressNames)
		'get the data from the text boxes and store in the object referenced by a
		a.FullName = txtName.Text
		a.EmailAddy = txtEmailAddress.Text
		a.PhoneNum = txtPhone.Text
		a.Notes = txtNotes.Text
	End Sub
	Private Sub AddRecord(ByVal a As AddressNames)
		'add the object referenced by a to the collection and use the full name as the key.
		Try
			emailCollection.Add(a, a.FullName)
		Catch ex As Exception
			MessageBox.Show(ex.Message)
		End Try
	End Sub
	Private Sub ClearForm()
		'clear the form yay
		txtName.Clear()
		txtEmailAddress.Clear()
		txtPhone.Clear()
		txtNotes.Clear()

		'focus pocus
		txtName.Focus()
	End Sub

	Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
		Me.Close()

	End Sub
End Class



next is the model
Module EmailCollectionModule
	Public emailCollection As Collection
End Module



last is a class
Public Class AddressNames
	'member vars
	Private strFullName As String
	Private strEmailAddy As String
	Private strPhoneNum As String
	Private strNotes As String

	'FullName property procedure
	Public Property FullName() As String
		Get
			Return strFullName
		End Get
		Set(ByVal value As String)
			strFullName = value
		End Set
	End Property
	'EmailAddy property
	Public Property EmailAddy() As String
		Get
			Return strEmailAddy
		End Get
		Set(ByVal value As String)
			strEmailAddy = value
		End Set
	End Property
	'phone property
	Public Property PhoneNum() As String
		Get
			Return strPhoneNum
		End Get
		Set(ByVal value As String)
			strPhoneNum = value
		End Set
	End Property
	'notes property
	Public Property Notes() As String
		Get
			Return strNotes
		End Get
		Set(ByVal value As String)
			strNotes = value
		End Set
	End Property
	Public Sub clear()
		'reset members variables to default values
		strFullName = String.Empty
		strEmailAddy = String.Empty
		strPhoneNum = String.Empty
		strNotes = String.Empty
	End Sub
End Class



thats all the code I have so far, i really just want the error fix. thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: Removing a selected index in a list box

#2 KyleG  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 44
  • Joined: 11-June 08

Re: Removing a selected index in a list box

Posted 21 July 2008 - 03:09 PM

This is just a guess, since I haven't tried to run your code. I think the Collection.Remove() method takes an integer index, not a string.
So I think this:
emailCollection.Remove(lstNames.SelectedIndex.ToString())




Should be this:
emailCollection.Remove(lstNames.SelectedIndex)



Was This Post Helpful? 0
  • +
  • -

#3 boohickmanjc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-July 08

Re: Removing a selected index in a list box

Posted 22 July 2008 - 10:31 AM

I did this and i got this in return "Collection index must be in the range 1 to the size of the collection." after i said yes.


View PostKyleG, on 21 Jul, 2008 - 03:09 PM, said:

This is just a guess, since I haven't tried to run your code. I think the Collection.Remove() method takes an integer index, not a string.
So I think this:
emailCollection.Remove(lstNames.SelectedIndex.ToString())




Should be this:
emailCollection.Remove(lstNames.SelectedIndex)



This post has been edited by boohickmanjc: 22 July 2008 - 10:34 AM

Was This Post Helpful? 0
  • +
  • -

#4 djkitt  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 189
  • Joined: 22-May 08

Re: Removing a selected index in a list box

Posted 22 July 2008 - 12:00 PM

Hey there boohickmanjc,

Quote

I did this and i got this in return "Collection index must be in the range 1 to the size of the collection." after i said yes.


The collections first item starts at one instead of zero, so try...
   emailCollection.Remove(lstNames.SelectedIndex + 1)  



I took a quick look at the rest of your code...

It looks like you are trying to:
Step 1) delete the item from the email collection
Step 2) delete the item from the listbox
Step 3) rebuild the listbox from the email collectiuon

My suggestion here would be to:
A) skip step 2
or
B)Change step 2 to use RemoveAt instead of Remove and then skip Step 3
 lstNames.Items.RemoveAt(intIndex)



Hope this helps,
Was This Post Helpful? 1
  • +
  • -

#5 boohickmanjc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-July 08

Re: Removing a selected index in a list box

Posted 22 July 2008 - 05:04 PM

Kitt, I changed the first thing you said to and it worked perfectly. Thanks :) Also thank you both for responding.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1