Remove element from array list and combobox......

D

Don

My user control has a combobox with an arraylist attached to it along with custom add and remove methods.

The "Add" method is working great. However I don't understand why the "Remove" method isn't working. It neither removes the item from the arraylist nor from the combobox like it's supposed to do.

A couple of notes: cbx is the name of the combobox within my usercontrol, file is the name of the arraylist within my usercontrol. To try and debug this issue, I added a public variable elsewhere in my usercontrol. This variable is named "test". When I "watch" this test variable as the remove method operates, it DOES get set to 0 which IS the element that needs to be removed. File.count AND cbx.count both equal 3.

Thanks for any assistance you may be able to provide.

--

Don

'This is the call to the add method that DOES work.
cbxMyComboBox.add("New Item")

'This is the add method that DOES work
Public Overridable Sub add(ByVal n As String)
'add item to the combobox
cbx.Items.Add(n)
File.Add(n)
noLocations = false
'select the new item in the combobox
If Not cbx.Items.Count = 0 Then
cbx.SelectedIndex = cbx.Items.Count - 1
End If
end sub

'This is the call the remove method that DOESN'T work
cbxMyControl.Remove(cbxMyControl.selectedindex)

'This is the Remove method that DOESN'T work.
Public Overridable Sub remove(ByVal i As Integer)
cbx.Items.Remove(i)
File.Remove(i)
Test = i
If Not cbx.Items.Count = 0 Then
cbx.SelectedIndex = 0
noLocations = False
Else
noLocations = True
End If
end sub
 
H

Herfried K. Wagner [MVP]

* "Don said:
The "Add" method is working great.  However I don't understand why the "Remove" method isn't working.  It
neither removes the item from the arraylist nor from the combobox like it's supposed to do.

A couple of notes:  cbx is the name of the combobox within my usercontrol, file is the name of the arraylist
within my usercontrol.  To try and debug this issue, I added a public variable elsewhere in my usercontrol. 
This variable is named "test".  When I "watch" this test variable as the remove method operates, it DOES get set
to 0 which IS the element that needs to be removed.  File.count AND cbx.count both equal 3.

You will have to pass a reference to the object you want to be removed
to the 'Remove' method, the index can be passed to the 'RemoveAt'
method, not to 'Remove'.
 
A

Antoine Cote [MSFT]

Hello Don,

the Remove method expects you to pass in the object to be removed. For your
example below, that would be the string "New Item", not the index of the
element to remove. If you want to remove an item at a specific index, you
can use the RemoveAt method instead.

HTH

Antoine
Microsoft Visual Basic .NET
--
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "Don" <[email protected]>
Subject: Remove element from array list and combobox......
Date: Sun, 14 Dec 2003 11:16:41 -0500
Lines: 183
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_00B3_01C3C233.BFCD9DB0"
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: pcp05997552pcs.benslm01.pa.comcast.net 68.81.60.128
Path: cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.
phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.vb:163977
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

My user control has a combobox with an arraylist attached to it along with custom add and remove methods.
The "Add" method is working great. However I don't understand why the
"Remove" method isn't working. It neither removes the item from the
arraylist nor from the combobox like it's supposed to do.
A couple of notes: cbx is the name of the combobox within my usercontrol,
file is the name of the arraylist within my usercontrol. To try and debug
this issue, I added a public variable elsewhere in my usercontrol. This
variable is named "test". When I "watch" this test variable as the remove
method operates, it DOES get set to 0 which IS the element that needs to be
removed. File.count AND cbx.count both equal 3.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top