473,325 Members | 2,608 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,325 software developers and data experts.

problem wih list box

i have a problem with listboxes , i should compare two list boxes(A&B) and

1) all items present in listbox1 and which are not present in listbox2 should apper in a third listbox (c) ie., i want A-B
2) all items present in both listboxes will appear 3 rd list box (ie., A+B)


pls give a relavant sample code so that i can work out the problem .


with regards and thanks
vijay
May 28 '07 #1
2 997
bergy
89
Well I'm assuming VB and that your list box items are simple string objects. If you're populating with other objects you're going to need to do a better if check than the one below:

Expand|Select|Wrap|Line Numbers
  1.         'loop through the first set of listbox items
  2.         For Each LstItem As String In ListBox1.Items
  3.             Dim itemIsInListTwo As Boolean = False
  4.             For Each LstItem2 As String In ListBox2.Items
  5.                'compare each item to each item in the second list box
  6.                 If LstItem = LstItem2 Then
  7.                     'item is in the second list box so set avariable to true
  8.                     itemIsInListTwo = True
  9.                 End If
  10.             Next
  11.             'if our variable is true, add the item to the third list box
  12.             If itemIsInListTwo = False Then ListBox3.Items.Add(LstItem)
  13.         Next
  14.  
This seems relatively easy, I hope this isn't a school project or something :P
May 28 '07 #2
mwalts
38
Well I'm assuming VB and that your list box items are simple string objects. If you're populating with other objects you're going to need to do a better if check than the one below:

Expand|Select|Wrap|Line Numbers
  1.         'loop through the first set of listbox items
  2.         For Each LstItem As String In ListBox1.Items
  3.             Dim itemIsInListTwo As Boolean = False
  4.             For Each LstItem2 As String In ListBox2.Items
  5.                'compare each item to each item in the second list box
  6.                 If LstItem = LstItem2 Then
  7.                     'item is in the second list box so set avariable to true
  8.                     itemIsInListTwo = True
  9.                 End If
  10.             Next
  11.             'if our variable is true, add the item to the third list box
  12.             If itemIsInListTwo = False Then ListBox3.Items.Add(LstItem)
  13.         Next
  14.  
This seems relatively easy, I hope this isn't a school project or something :P
Depending on the size of the listboxes, you might want to change that second foreach for a normal for loop, still iterating through the list of items but with control logic up top that escapes the inner loop when itemIsInListTwo = true. Alternatively use a break statement before the End If (but if it is a school project, most instructors prefer the control logic variant :p)

Have fun,

-mwalts
May 28 '07 #3

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

Similar topics

5
by: Clifford W. Racz | last post by:
Has anyone solved the issue of translating lists in Word 2003 (WordML) into xHTML? I have been trying to get the nested table code for my XSLT to work for a while now, with no way to get the...
10
by: Michael Strorm | last post by:
Hi! I've been having problems with a DTD. Having had the Sun XML validator reject a document, I put it through 'xmllint' for more information. 'Xmllint' noted a problem with the DTD itself;...
2
by: Pascal Deparis | last post by:
Hi, I've got a problem with the DB2 Command Line Processor, in V8, FP1. The databases have been created on an AIX server (version 4.3.3). Have a look at this: >db2 ? list DB21034E The...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
1
by: cody | last post by:
I have a OOP problem with the well known pattern where objects containing an object which represents a list of subobjects. Now my problem is that the ctor of a subobject indirectly calls the...
14
by: rurpy | last post by:
Another Python docs problem... I was trying to use imp.find_module(). >>> imp.find_module("mymod", "./subdir") ImportError: No frozen submodule named ./subdir.mymod subdir/mymod.py...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
7
by: Fernando Barsoba | last post by:
Hi, After following the advice received in this list, I have isolated the memory leak problem I am having. I am also using MEMWATCH and I think it is working properly. The program does some...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.