Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scan a listbox 1

Status
Not open for further replies.

ictoo

Programmer
Sep 3, 2009
33
GB
Hey,

I have another question concerning listboxes, I have data being added to a DB and then that being displayed on a listbox, now I’d like to make sure that my users can’t add the same file twice to the same list box. I assume SCAN is going to be needed but I’ve not used it before. If you can point me in the right direction I would be much appreciated.

Also the listboxes are associated to a task so each task has its own files pickup by a key from the tasks, so you couldn’t scan the DB it would have to be the list or make sure it only scanned files with the key of the task in.
 
Ictoo,

This is quite easy. Just scan the listbox's List property, which is simply an array containing all the values in the control.

So, to find the value "XYZ", you could do something like this:

Code:
FOR lnI = 1 TO THIS.ListCount
  IF THIS.List(lnI) = "ZYX"
    * Do something with the found value
    EXIT
   ENDIF
ENDFOR

You can also treat the property as a two-dimensional array if the listbox has multiple columns.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
if you bind your listbox to an alias you can also simply scan the alias, or locate or seek or indexseek for a value before adding it and only adding it, if it isn't there to prevent double values.

Bye, Olaf.
 
Thanks for the help i'll be trying these solutions tomorrow, my listbox sourcetype is fields, or your solution would work, i think thats what i was planning on doing but it didnt work, i'll be trying mikes tomorrow.

thanks for all your help.
 
If your source is fields, that's quite the same as an alias in that respect. Indexseek with the parameter to move the recordpointer set to .F. should do the trick. Of course you need an appropriate index on the alias to indexseek() for the existance of a doublette value, before it will be inserted.

Bye, Olaf.
 
I would guess that it would be faster to scan the List property rather than the underlying table, given that the values in the List are held entirely in memory.

Then again, the table is likely to be small, so maybe it won't make any significant difference.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Mike,

I doubt the list of the listbox is in memory, if you set it's rowsource as alias or fields, it simply looks up rows from the alias. And even if it also does copy the alias fields to it's internal array, then by accessing the alias, the alias will be fully cached too, wouldn't it?

The indexseek has the advantage to just lookup in the index, which at that stage should be faster, and not move the record pointer too, to not influence the active item(s).

Bye, Olaf.
 
Sorry to bring this back up again, but i never fully finished this project as something bigger came up.

The code you showed me mike doesn’t seem to work with the way im using it.

FOR lnI = 1 TO thisform.pageframe1.page3.List1.ListCount
IF thisform.pageframe1.page3.List1.List(lnI) = "CHANGES TO ER SYSTEM.DOC"
MESSAGEBOX("found")
RETURN
ELSE
MESSAGEBOX("not found")
RETURN
ENDIF
ENDFOR

in the IF i will change the = to a variable that will be gotten when the user selects a file they want to add to the listbox something like justfname(lcgetfile).

Now when i run this code and i know there is a file in the list thats a duplicate it say "not found" so it means i cant find it, ive tried alot of different things to look for and still no luck, my list box is set to alias but was fields and its showing as its rowsource as : projectlistfiles.descno,filename this is so i can add this files to list box and then the user just clicks the file name and it will launch that file from the descno that’s shows the full path. This descno is hidden and only filename is shown.

Once again if you could enlighten me I would appreciate it so much.

Thanks, Ictoo.
 
Ictoo,

A couple of things I would always do in this situation:

- Trim any leading and trailing spaces from the string being tested;

- Make it case-insensitive.

In other words:

IF ALLTRIM(UPPER( thisform.pageframe1.page3.List1.List(lnI) )) = etc.

I'm not saying that's necessarily the solution in this case, but it's worth checking before going any further.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Ive looked in to it more and the

?thisform.pageframe1.page3.List1.ListCount is showing 4 as there are 4 items in the list and,

?thisform.pageframe1.page3.List1.List(lnI) is showing "0019" thats the descno that is used to point to the filepath but dosnt store it sorry, so its just sitting on that top record of the list and if i do

thisform.pageframe1.page3.List1.List(lnI) = "0019" it finds it but any other descno i put in that are in the lost it wont find, and is there a way of making it look at filename and not descno?
 
Wait, I've just seen the cause of the problem.

The test will only be performed the first time round the loop. Whatever the outcome, the loop will exit after the first test. If the target term is present in the second or subsequent item in the list, it will never be found.

The following will fix it:

Code:
FOR lnI = 1 TO thisform.pageframe1.page3.List1.ListCount
  IF thisform.pageframe1.page3.List1.List(lnI) = ;
    "CHANGES TO ER SYSTEM.DOC"
    MESSAGEBOX("found")
    RETURN
  ENDIF
ENDFOR
MESSAGEBOX("not found")

But keep the ALLTRIM(UPPER()) I showed in my previous post as well.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Yet again, thanks alot mike ive been looking at this for the past 4 hours now, thanks alot for all your help.

I just need to get it to lookup the filename now, i think i can do that by changeing the way the files are executed, think i can just let it look up the file name instend of the unique ID as all the file names will be dependant on the file thats added so there should be no problem when looking up them.
 
As your rowsource is "projectlistfiles.descno,filename", the List() array is two dimensional. List(lnI,1) will have the descno, List(lnI,2) will have the filename.

But as you have your values in an alias you can use standard sql solutions to detect double filenames, eg select count(*) ... group by filename having count(*)>1.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top