473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problems with List box programming

Hi,

I would like to know how an item can be removed from a list box which contains more than one item. I'm getting an error if select 2nd or 3rd item for removal.
Please help me
Feb 28 '08 #1
11 1892
debasisdas
8,127 Expert 4TB
please try to use

Expand|Select|Wrap|Line Numbers
  1. list1.removeitem list1.listindex
Feb 28 '08 #2
Killer42
8,435 Expert 8TB
Remember that the index goes from 0 to one less than the number of items. So if you are trying to remove the last one, you may be using too high a number. What error do you get? And what version of VB is this?
Feb 28 '08 #3
Hi Mr.Killer42,

I'm using VB 6.0 and is getting the fault message as" run time error 381 and invalid property array index.

regards
Feb 28 '08 #4
debasisdas
8,127 Expert 4TB
kindly post the code you are working on.
Feb 28 '08 #5
Hi,

I'm making a programme in vb6.0 and got stuck up at a place where I want to remove items from a list box by pressing a command button after selecting the item in list box. I'm able to remove if only one item is there otherwise getting an error as " run time error 381 and invalid property array index." the code I tried is

Expand|Select|Wrap|Line Numbers
  1. Dim i
  2. For i = 0 To List8.ListCount - 1
  3.   If List8.Selected(i) Then
  4.     List8.Removeitem List8.Listindex
  5.   End If
  6. Next i
please help me
Feb 28 '08 #6
debasisdas
8,127 Expert 4TB
Are you trying to remove all the items from the list ?
Feb 28 '08 #7
Are you trying to remove all the items from the list ?
No. One by one at random. The main problem if I remove item with index 0
Feb 28 '08 #8
debasisdas
8,127 Expert 4TB
try this

Expand|Select|Wrap|Line Numbers
  1. if list1.listindex<> -1 then
  2. list1.removeitem list1.listindex
  3. end if
  4.  
Feb 28 '08 #9
Killer42
8,435 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. Dim i
  2. For i = 0 To List8.ListCount - 1
  3.   If List8.Selected(i) Then
  4.     List8.Removeitem List8.Listindex
  5.   End If
  6. Next i
I think this code will work, if you reverse the For loop as shown below. If you think about it, you are counting from, say, 0 to 5. But if you remove an item along the way, all the rest will shift back one place. So not only will you skip over the subsequent one, but you will try to go one place too far.
Expand|Select|Wrap|Line Numbers
  1. Dim i
  2. For i = List8.ListCount - 1 To 1 Step -1
  3.   If List8.Selected(i) Then
  4.     List8.Removeitem i
  5.   End If
  6. Next i
Note, I also changed the index of the item to be removed to i. I believe this will work better if you allow multiselect in the listbox.
Feb 29 '08 #10
I think this code will work, if you reverse the For loop as shown below. If you think about it, you are counting from, say, 0 to 5. But if you remove an item along the way, all the rest will shift back one place. So not only will you skip over the subsequent one, but you will try to go one place too far.
Expand|Select|Wrap|Line Numbers
  1. Dim i
  2. For i = List8.ListCount - 1 To 1 Step -1
  3.   If List8.Selected(i) Then
  4.     List8.Removeitem i
  5.   End If
  6. Next i
Note, I also changed the index of the item to be removed to i. I believe this will work better if you allow multiselect in the listbox.
Hi Killer42,

Thanx a lot. It is working perfectly with a small change. The for loop lower limit has to be changed to 0 instead of 1 as otherwise the first one is not getting removed.

Regards
Feb 29 '08 #11
Killer42
8,435 Expert 8TB
It is working perfectly with a small change. The for loop lower limit has to be changed to 0 instead of 1 ...
Good catch! :)
Mar 2 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: dan glenn | last post by:
I'm creating HTML emails from a PHP site and sending them out to an email list (just about 40 people so far are on this list). I've tested and confirmed that these emails work in yahoo.com's...
18
by: jblazi | last post by:
I should like to search certain characters in a string and when they are found, I want to replace other characters in other strings that are at the same position (for a very simply mastermind game)...
4
by: vivek | last post by:
Hi all, Here we are conducting a QIP program for teachers in other colleges. I am assigned the job of taking lectures in Python. Although there will be not much time (exactly speaking : 4 hours)...
8
by: Andrew Edwards | last post by:
The following function results in an infinite loop! After 5+ hours of debugging, I am still unable to decipher what I am incorrectly. Any assistance is greatly appreciated. Thanks in advance,...
14
by: Jim Hubbard | last post by:
Are you up to speed on the difficulties in using the 1.1 .Net framework? Not if you are unaware of the 1,596 issues listed at KBAlertz (http://www.kbalertz.com/technology_3.aspx). If you are...
12
by: Stephen Tyndall | last post by:
I'm not sure whether this counts as off-topic, but here's a huge list of books about C++ that I've found highly recommended (kind of slanted towards game programming because that's what I'm looking...
0
by: Peter R. Vermilye | last post by:
I am involved on a web application that is using a third party set of APIs for remote database access (middleware). I've been brought in because of my background in programming, thus I'm new to...
6
by: Dreamcatcher | last post by:
Hello, I'm trying to learn some C, reading my book, Beginning Linux Programming I came across the following program. The program is supposed to walk through directory's and print all its...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
12
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.